Alternative: Component Composition to Avoid Drilling
What you'll learn: How to use React's children and component slots to bypass prop drilling by passing components instead of data.
The Core Idea
Instead of passing data down through multiple component layers, you can pass entire components as props (often as children). The parent component renders the child directly, so data only needs to travel from parent to child—skipping all the intermediate "middleman" components entirely.
How It Works
Think of it like a delivery service. With prop drilling, your package (data) gets handed from person to person down a chain. With component composition, you pack the delivery driver inside the truck at the start—they travel together directly to the destination, no handoffs needed.
// ❌ Prop drilling version
<Layout>
<Sidebar userName={userName} />
</Layout>
// Inside Sidebar, userName gets passed again...
// ✅ Composition version
<Layout>
<Sidebar>
<UserProfile userName={userName} />
</Sidebar>
</Layout>
In the second example, Sidebar doesn't need to know about userName at all. It just renders its children. The UserProfile component is created in the parent where userName already exists.
Named Slots Pattern
You can also pass multiple components as named props:
<Dashboard
header={<Header userName={userName} />}
sidebar={<Sidebar />}
/>
Now Dashboard doesn't handle user data—it just positions the pre-built components.
Key Takeaway: When intermediate components don't actually use the data they're passing, consider passing pre-configured components as children or props instead—composition eliminates drilling without Context.