Course contentsShow
← React
Lesson 1074 of 2,39627. Composition: Render PropsFree lesson

Combining Both: Hooks for Logic, Render Props for UI

Using custom hooks internally while exposing render props for consumer customization of presentation.

Combining Both: Hooks for Logic, Render Props for UI

You'll learn how to use custom hooks internally for logic while exposing render props to let consumers control presentation.

The Best of Both Worlds

Hooks and render props aren't enemies—they're complementary. A powerful pattern is to use custom hooks internally to manage stateful logic, then expose a render prop to give consumers full control over how that logic appears in the UI.

Think of it like a restaurant kitchen: the hook is your professional chef handling all the cooking logic (timing, temperature, ingredients), while the render prop is the plating station where the customer decides exactly how they want their meal presented.

How It Works

Your component uses hooks to encapsulate logic, then passes the resulting state and handlers to a render prop:

function DataFetcher({ url, render }) {
  // Hook handles the logic
  const { data, loading, error } = useFetch(url);
  
  // Render prop handles the UI
  return render({ data, loading, error });
}

// Consumer has full presentation control
<DataFetcher 
  url="/api/users"
  render={({ data, loading, error }) => (
    loading ? <Spinner /> :
    error ? <ErrorBox error={error} /> :
    <UserList users={data} />
  )}
/>

Why This Pattern?

You get encapsulation from hooks (the complex logic is hidden and testable) plus flexibility from render props (consumers decide exactly what to render). The component author manages what happens; the consumer controls how it looks.

This is especially valuable for library authors: your library handles the hard parts (data fetching, animations, subscriptions) while developers using your library maintain complete creative control.

Key Takeaway: Use hooks to encapsulate complex logic internally, then expose render props to give consumers complete control over presentation—combining the strengths of both patterns.