Basic Render Prop Implementation
What you'll learn: How to create a component that accepts a function as a prop and calls it to render UI with data.
The Core Idea
A render prop is simply a prop whose value is a function. Your component calls this function and uses whatever JSX it returns. This lets the parent decide what to render while your component provides the data to render with.
Think of it like a restaurant kitchen: your component (the kitchen) prepares the ingredients (data), but the render prop function (the chef's plating instructions) decides how to arrange them on the plate.
Basic Implementation Pattern
function DataProvider({ render }) {
const data = "Hello from DataProvider";
return render(data);
}
// Usage:
function App() {
return (
<DataProvider
render={(data) => <h1>{data}</h1>}
/>
);
}
Here's what happens:
DataProviderreceives arenderprop (a function)- It prepares some data (
"Hello from DataProvider") - It calls the render function, passing the data as an argument
- It returns whatever JSX the render function produces
Why This Pattern Matters
The parent component gets complete control over presentation while the DataProvider handles the logic and data. You can reuse DataProvider with totally different UIs:
<DataProvider render={(data) => <p className="text-small">{data}</p>} />
<DataProvider render={(data) => <div><strong>{data}</strong></div>} />
Same component, different renders!
Key Takeaway: A render prop component accepts a function prop, calls it with data, and returns the result—separating data logic from presentation decisions.