Course contentsShow
← React
Lesson 274 of 2,3968. Props PatternsFree lesson

Preview of Solutions: Composition

Understand how component composition (children prop, slots) can eliminate drilling by moving structure up.

Preview of Solutions: Composition

What you'll learn: How component composition can eliminate prop drilling by restructuring where components live in your tree.

The Core Idea

Instead of passing props down through multiple layers, move the deeply nested component up to where its data lives, then pass the whole component down as children or another prop. This way, intermediate components never see the props—they just render whatever structure they receive.

How It Works

Imagine you're drilling user through Page → Sidebar → UserMenu. Instead:

// Before: Drilling user through Sidebar
<Page user={user}>
  <Sidebar user={user} />
</Page>

// After: Sidebar receives composed content
<Page>
  <Sidebar>
    <UserMenu user={user} />
  </Sidebar>
</Page>

Now Page doesn't need to know about user, and Sidebar just renders its children. The UserMenu gets user directly where it was created—no drilling.

The Analogy

Think of it like packing a suitcase. Drilling is like telling each person in a chain exactly what's inside so they can repack it. Composition is like handing someone a locked suitcase—they just carry it without needing to know the contents.

When Composition Solves Drilling

  • When intermediate components don't use the props themselves
  • When you control the component structure
  • When the "hole" in your component (children/slots) makes semantic sense

Composition doesn't work everywhere, but it's often the cleanest solution when it fits.

Key Takeaway: Component composition eliminates prop drilling by moving structure decisions upward, letting you pass assembled UI rather than raw data through intermediate layers.