Basic Render Prop Syntax
What you'll learn: How to pass a function as a prop that returns JSX, allowing a component to share data by calling that function.
The Core Idea
A render prop is simply a function passed as a prop that tells a component what to render. Instead of the component deciding how to display data, it hands that responsibility to the parent by calling the function and using whatever JSX it returns.
Think of it like a restaurant where you bring your own chef. The restaurant (component) provides the ingredients (data), but your chef (the render prop function) decides how to prepare and plate the meal (JSX).
How It Works
The component receiving the render prop does three things:
- Accepts a function as a prop (commonly named
renderorchildren) - Calls that function with any data it wants to share
- Returns the result as part of its JSX
The parent component passes a function that:
- Receives the data as arguments
- Returns JSX that uses that data however it wants
// Component with render prop
function DataProvider({ render }) {
const data = "Hello from DataProvider";
return render(data); // Call the function with data
}
// Parent using it
<DataProvider render={(data) => <h1>{data}</h1>} />
The parent has complete control over how the data appears, while DataProvider controls what data to share.
Key Takeaway: A render prop is a function passed as a prop that returns JSX—the component calls it with data, and you control the rendering output.