Course contentsShow
← React
Lesson 1077 of 2,39628. Composition: Compound ComponentsFree lesson

What Are Compound Components?

Components that work together to form a cohesive API, sharing implicit state without prop drilling.

What Are Compound Components?

You'll learn what compound components are and why they're a powerful pattern for building flexible, user-friendly APIs in React.

The Core Idea

Compound components are a collection of components that work together to form a complete, cohesive UI element. They share implicit state behind the scenes without requiring you to drill props through multiple layers.

Think of them like a team sport: each player (component) has a specific role, but they all share awareness of the game state without constantly passing the ball to each other. The coach (parent component) orchestrates them, but the players communicate through shared understanding.

Why This Matters

Without compound components, you often face two painful choices:

  1. Prop drilling hell: Pass every piece of state down through multiple levels
  2. Monolithic components: Cram everything into one massive component with limited customization

Compound components solve this by letting child components access shared state implicitly—they "just know" what they need from their parent context.

Real-World Example: Tabs

Imagine a <Tabs> component. Instead of:

<Tabs tabs={[...]} activeTab={0} onTabChange={...} />

Compound components let you write:

<Tabs>
  <TabList>
    <Tab>First</Tab>
    <Tab>Second</Tab>
  </TabList>
  <TabPanel>Content 1</TabPanel>
  <TabPanel>Content 2</TabPanel>
</Tabs>

Each piece knows its role and shares state (which tab is active) without explicit props between them.

The Benefit

You get flexibility in composition—rearrange, customize, or extend—while maintaining a clean, intuitive API. Users of your component can see the structure clearly without drowning in prop complexity.

Key Takeaway: Compound components let related components share state implicitly, creating flexible APIs that feel natural to use while avoiding prop drilling.