React Render Props Pattern β Reusable Logic via Functions (2025 Guide)
Introduction β What Is the Render Props Pattern?
Before Hooks, React developers often reused logic using Higher-Order Components (HOCs) or the Render Props pattern. Render Props allows you to share logic across components by passing a function as a prop that returns JSX.
It gives you fine-grained control over rendering while still encapsulating logicβespecially useful for things like:
- Tracking mouse position
- Form state
- Toggle logic
- Data fetching or subscriptions
In this guide, youβll learn:
- What Render Props are and how to use them
- How to pass and call render functions
- When to prefer Render Props over HOCs
- Modern alternatives and best practices
1. What is a Render Prop?
A Render Prop is a prop whose value is a function that returns JSX. It’s commonly named render, children, or component.
Syntax:
<MyComponent render={(data) => <OtherComponent data={data} />} />
Enables dynamic rendering logic
Logic is contained in the provider component, UI comes from the consumer
2. Classic Render Props Example
MouseTracker Component:
class MouseTracker extends React.Component {
state = { x: 0, y: 0 };
handleMouseMove = (e) => {
this.setState({ x: e.clientX, y: e.clientY });
};
render() {
return (
<div onMouseMove={this.handleMouseMove} style={{ height: '100vh' }}>
{this.props.render(this.state)}
</div>
);
}
}
Usage:
<MouseTracker render={({ x, y }) => (
<h2>The mouse position is ({x}, {y})</h2>
)} />
Reuses mouse tracking logic
Consumer controls how to render the state
3. Functional Component Render Props (with Children)
Render props work well with the children prop too:
const Toggle = ({ children }) => {
const [on, setOn] = useState(false);
const toggle = () => setOn((o) => !o);
return children({ on, toggle });
};
Usage:
<Toggle>
{({ on, toggle }) => (
<button onClick={toggle}>
{on ? 'ON' : 'OFF'}
</button>
)}
</Toggle>
children as a function is idiomatic and flexible
4. Render Props vs HOCs vs Hooks
| Feature | Render Props | HOC | Hook (Modern) |
|---|---|---|---|
| Logic reuse | Yes | Yes | Yes |
| Syntax style | JSX-in-function | Component wrapper | Hook inside component |
| Nesting | Can become deep | Wrapper hell | Clean and flat |
| Class support | Yes | Yes | Hooks = functional only |
| Modern usage | Rare (legacy/edge) | Rare (mostly replaced) | Preferred pattern |
5. Common Use Cases for Render Props
| Use Case | Logic Shared |
|---|---|
| π±οΈ Mouse position | Track mouse events, return coords |
| Toggle state | Boolean on/off + toggle function |
| Form state | Handle form values & handlers |
| Data fetching | Inject fetched data and status |
| Media queries | Inject responsive flags |
6. Avoid “Render Props Hell”
<Data>
{(data) => (
<Theme>
{(theme) => (
<Auth>
{(user) => (
<Component user={user} theme={theme} data={data} />
)}
</Auth>
)}
</Theme>
)}
</Data>
Deeply nested render props are hard to read and maintain
Prefer custom Hooks or composition for better structure
Best Practices
Use clear names like render, children, or renderContent
Document the shape of arguments passed to the render function
Avoid nesting more than 2β3 levels
Consider using Hooks if available
Combine with memoization (useMemo, React.memo) if needed
Summary β Recap & Next Steps
The Render Props pattern is a classic React solution for sharing stateful logic across components. Itβs flexible and expressive, but in modern apps, Hooks are preferred for cleaner syntax and better scalability.
Key Takeaways:
- A render prop is a function passed as a prop that returns JSX
- Use it to separate logic (provider) from UI (consumer)
- Ideal for toggles, tracking, and form state
- Avoid deeply nested render functions
- Hooks are the modern replacement, but render props are still useful
Real-World Relevance:
Used in legacy libraries (like react-router, downshift) and in class-based codebases where Hooks are not an option.
FAQ Section
Is Render Props still relevant in 2025?
Less common, but still useful in class components or third-party libraries. Hooks are preferred for new development.
What’s the difference between a render prop and a component prop?
A render prop is a function returning JSX. A component prop is a component type (like <Route component={...} />).
Can I use render props in functional components?
Yes. You can use children or render as function props in any component.
How do I avoid nested render props?
Use custom hooks, composition, or context. Extract nested logic into reusable utilities.
Should I replace all HOCs/render props with Hooks?
If you’re using functional components and modern React, Hooks are cleaner and more scalable.
Share Now :
