Composition to Avoid Prop Drilling
What you'll learn: How to use the children prop and component composition to pass state down without threading props through intermediate components.
The Core Idea
When you lift state down to avoid unnecessary re-renders, you sometimes create a new problem: components in between need props just to pass them along. Instead of drilling props through layers, you can use composition—wrapping components with state-dependent children and passing them as the children prop.
Think of it like a postal service. Instead of having every person along the route open the package, check the address, and repack it (prop drilling), you can put the package in a sealed container (children) that travels through the route untouched until it reaches its destination.
How It Works
Rather than passing props down through <Middle> to reach <Bottom>, you render <Bottom> where the state lives, then pass it as children:
// Before: prop drilling
<Parent>
<Middle someProp={value} /> {/* just passing through */}
</Parent>
// After: composition
<Parent>
<Middle>
<Bottom someProp={value} /> {/* rendered at parent level */}
</Middle>
</Parent>
Now Middle receives children and simply renders them. It doesn't know or care about someProp. The state and the component that needs it stay close together, while Middle becomes a pure layout wrapper.
This pattern shines when you've lifted state down but still need to reach components nested in the JSX tree. Instead of lifting state back up (undoing your optimization), you restructure your component boundaries to pass rendered elements through.
Key Takeaway: Use the children prop and composition to bypass intermediate components—keeping state close to where it's needed without forcing every layer to handle props they don't use.