Passing Arguments to Function Children
What you'll learn: How parent components pass data—like state or computed values—down to function children, letting the parent control what the child can access.
The Core Idea
When you use the children-as-function pattern, the parent component calls the function it receives and can pass arguments into it. This is how the parent exposes its internal state, methods, or computed values to the render logic defined by the consumer.
Think of it like a waiter bringing you a menu. The restaurant (parent) decides what options appear on that menu, but you (the function child) decide how to order from it.
How It Works
The parent component invokes props.children like a regular function:
function DataProvider({ children }) {
const [count, setCount] = useState(0);
// Call children as a function, passing data as arguments
return children(count, setCount);
}
The consumer receives those arguments and uses them to render:
<DataProvider>
{(count, setCount) => (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
)}
</DataProvider>
The parent decides what to expose (count and setCount), while the consumer decides how to display it.
Why This Matters
This pattern gives the parent full control over its internal logic while letting consumers customize the UI. The parent encapsulates complexity—the consumer just receives clean, ready-to-use data.
Key Takeaway: Parent components invoke function children with arguments to expose internal state or computed values, giving consumers access to data while maintaining encapsulation.