The 'children as Function' Variant
What you'll learn: How to use children as your render prop for cleaner, more readable component syntax.
In the previous lesson, you saw the render prop pattern with an explicit prop like render. But there's a popular variant: instead of a dedicated prop, you can pass the function as children. This means using the component's opening and closing tags with a function inside.
The Core Idea
Rather than:
<DataFetcher render={data => <div>{data}</div>} />
You write:
<DataFetcher>
{data => <div>{data}</div>}
</DataFetcher>
The component receives this function through props.children, then calls it exactly like before:
function DataFetcher({ children }) {
const data = 'Hello from DataFetcher';
return children(data); // Call the function!
}
Why This Feels Better
It reads more naturally. The consuming code looks like normal component composition—there's visual hierarchy with opening/closing tags. The function you're passing feels like the "content" of the component, which often matches its purpose.
It's functionally identical to render={...}, just syntactically different. You're still passing a function that receives data and returns JSX.
When to Use Which
Use children as function when:
- The function is the main thing your component needs
- You want that "wrapping" visual pattern
Use an explicit render prop when:
- You need multiple render functions
- You want to pass other children alongside it
Key Takeaway: The children prop can receive a function just like any prop, creating a cleaner syntax for the render prop pattern: <Component>{data => <UI />}</Component>.