π 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 :