Render Props is a React pattern that allows components to share logic by passing a function as a prop. The receiving component calls this function to render content dynamically, enabling code reuse while keeping the UI flexible.
Syntax
const DataProvider = ({ render }) => render("Hello, Render Props!");
};
const App = () => <DataProvider render={(message) => <h1>{message}</h1>} />;
How it works?
- The DataProvider component takes a render prop, which is a function.
- It calls render(data), passing the data to the function.
- The App component passes a function as a prop that renders h1 with the received message.
- This allows the logic of fetching data to be separated from how the UI renders it.
Now let's understand with a simple example
JavaScript
import React from 'react';
const MouseTracker = (props) => {
const [mousePosition, setMousePosition] = React.useState({ x: 0, y: 0 });
const handleMouseMove = (event) => {
setMousePosition({ x: event.clientX, y: event.clientY });
};
return (
<div style={{ height: '100vh' }} onMouseMove={handleMouseMove}>
{props.render(mousePosition)}
</div>
);
};
const App = () => {
return (
<MouseTracker render={(position) => (
<h1>Mouse Position: {position.x}, {position.y}</h1>
)} />
);
};
export default App;
Output:
Render PropsIn this example
- MouseTracker maintains the mouse position in its state.
- Passing the mouse position as an argument, it calls the render function prop
- App component provides the function that renders the mouse position.
Why Use Render Props?
Render Props provides several advantages, including:
- Code Reusability: Instead of duplicating logic across components, you can encapsulate it in one place and use a render function to dictate how the UI looks.
- Separation of Concerns: It separates business logic from UI components, making the codebase cleaner and more maintainable.
- Greater Flexibility: Unlike Higher-Order Components (HOCs), Render Props allow you to customize the way components are rendered dynamically.
Note: While Render Props was a popular pattern in React for sharing reusable logic, it is now less common in modern React development. In many cases, Custom Hooks have replaced Render Props due to their simplicity and improved readability.
Passing Functions as Props
This example demonstrates the render props pattern in React where a function is passed to child component. The child component uses the render prop and display additional content.
JavaScript
import React from "react";
class App extends React.Component {
render() {
return (
<div>
<h1>Render Props Example</h1>
<SampleRenderProps />
</div>
);
}
}
// Child component getting render props
class RenderPropsComponent extends React.Component {
render() {
return (
<div>
<h2>I am Child Component</h2>
{this.props.render()}
</div>
);
}
}
// Parent component sending render props to the child
class SampleRenderProps extends React.Component {
render() {
return (
<RenderPropsComponent
render={() => {
return (
<div>
<h3>I am coming from render props</h3>
</div>
);
}}
/>
);
}
}
export default App;
Output

In this example
- SampleRenderProps passes a render function to RenderPropsComponent.
- RenderPropsComponent calls this.props.render() inside JSX.
- Parent controls what gets rendered inside the child dynamically.
- It enables flexible and reusable UI rendering.
Use Cases for Render Props
- Mouse Tracking: Dynamically manages the mouse cooridnates.
- Form Handling: Efficiently manages the form state.
- Authentication: Based on Authentication State, they render UI.
- Data Fetching : For fetching the API, they reuse the logic.
- Animation Control: Dynamically manages the animations.
Render Props vs. Higher-Order Components (HOCs) vs. Custom Hooks
Both Render Props and HOCs aim to share reusable logic among components, but they do so differently. With Hooks, React now offers an even better alternative.
Feature | Render Props | Higher-Order Components (HOC) | Custom Hooks |
---|
Approach | Passes a function as a prop | Wraps a component and injects props | Uses a function that manages state and logic |
Readability | Can lead to function nesting | Can lead to wrapper hell | Clear and concise |
Performance | May cause re-renders if function is created inline | Potential for prop conflicts | Optimized for reuse |
Conclusion
Render Props is a powerful and flexible pattern in React that allows developers to share logic among components efficiently. However, with the introduction of React Hooks, Render Props have become less common and are often replaced by Custom Hooks, which offer better readability and performance.
Similar Reads
ReactJS Props Reference In React, one of the key concepts that make it flexible and powerful is props. Props, short for "properties", are used to pass data from one component to another. They enable React components to be dynamic and reusable, allowing data to flow down the component tree.In this article, weâll take a clos
2 min read
ReactJS render() Method In React, lifecycle methods manage a componentâs behaviour at different stages. The render() method is important for defining the UI, updating it whenever state, props, or context changes, and ensuring the UI stays in sync with the componentâs data.What is the Render() Method in ReactJS?The render()
6 min read
ReactJS Props - Set 1 The react props refer to properties in react that are passed down from parent component to child to render the dynamic content.Till now we have worked with components using static data only. In this article, we will learn about react props and how we can pass information to a Component.What are Prop
5 min read
ReactJS Props - Set 2 In our previous article ReactJS Props - Set 1 we discussed props, passing and accessing props, passing props from one component to another, etc. In this article, we will continue our discussion on props. So, what if we want to pass some default information using props to our components? React allows
4 min read
React Rebass Props React Rebass is a front-end framework that was designed keeping react in mind. In this article, we will know what are Props in React Rebase. The prop is an important component that is required in each development.Props are used for setting design constraints, ensuring easy accessibility to designs.
4 min read