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

Combining Render Props with Hooks

Modern patterns use hooks for logic extraction and render props only when truly needed for rendering control.

Combining Render Props with Hooks

You'll learn how to use hooks for internal logic while keeping render props for rendering flexibility.

The Modern Hybrid Pattern

With hooks in your toolkit, you might wonder: "Do I still need render props?" The answer is selective yes. Here's the strategy:

Use hooks to extract and manage stateful logic internally. This keeps your component clean and makes the logic reusable elsewhere. Use render props only when you need to give consumers explicit control over rendering.

Think of it like a car's design: hooks are the engine (hidden, reusable logic), and render props are the customizable exterior (visible, user-controlled presentation).

When to Combine Them

If you have a component that manages complex state or side effects, handle that with hooks inside. Then, if different consumers need to render that data differently, expose a render prop:

function DataFetcher({ url, render }) {
  // Hook handles the logic
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch(url)
      .then(res => res.json())
      .then(setData)
      .finally(() => setLoading(false));
  }, [url]);

  // Render prop gives consumer control
  return render({ data, loading });
}

// Usage
<DataFetcher 
  url="/api/users" 
  render={({ data, loading }) => 
    loading ? <p>Loading...</p> : <ul>{data.map(...)}</ul>
  } 
/>

The hook manages the "how" (fetching logic), the render prop manages the "what" (UI presentation).

The Decision Point

Ask yourself: "Does the consumer need rendering control, or just access to logic?" If just logic, extract a custom hook. If rendering varies significantly, add a render prop on top.

Key Takeaway: Use hooks for internal state/logic management, and add render props only when consumers genuinely need different rendering strategies—combining both gives you powerful, flexible components without overcomplicating simple cases.