When Render Props Beat Composition
You'll learn: When render props solve problems that fixed component composition can't handle cleanly.
The Flexibility Problem
Component composition is powerful when you know what components you want to nest together. But sometimes you need to share logic without deciding how it will be displayed. That's where render props shine.
Where Composition Falls Short
Imagine you have a MouseTracker component that tracks mouse position. With composition, you'd wrap specific UI:
<MouseTracker>
<Cursor />
</MouseTracker>
But what if you want the same tracking logic with different displays? A tooltip? A custom cursor? A coordinate display? You'd need separate wrapper components for each use case, duplicating the tracking logic.
The Render Prop Advantage
Render props let you separate the logic from the presentation:
<MouseTracker render={(x, y) => <Cursor x={x} y={y} />} />
// OR
<MouseTracker render={(x, y) => <Tooltip x={x} y={y} />} />
// OR
<MouseTracker render={(x, y) => <div>({x}, {y})</div>} />
Same tracking logic, infinite presentation options. The consumer decides what to render with the data.
Think of It Like This
Composition is like ordering a fixed meal at a restaurant. Render props are like getting ingredients and cooking them yourself. When you need different dishes from the same ingredients, render props give you that flexibility.
When to Choose Render Props
- Multiple consumers need the same logic with different UI
- You can't predict all use cases upfront
- The logic provides data that consumers use differently
- You're building a library for other developers
Key Takeaway: Render props beat composition when you need to share stateful logic without dictating the presentation layer—composition picks the components, render props provide the data and let consumers decide how to display it.