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

Why Compound Components Need Shared State

Understanding the problem: parent and children must coordinate without explicit prop drilling through every layer.

Why Compound Components Need Shared State

You'll learn why compound components must coordinate internally without forcing users to manually pass state through every child.

The Coordination Problem

When you split a component into compound parts (like <Tabs>, <TabList>, <Tab>, <TabPanel>), those parts need to work together seamlessly. The <Tab> needs to know if it's active. The <TabPanel> needs to know if it should display. The parent <Tabs> manages which tab is selected.

But here's the challenge: users shouldn't have to wire this up manually. If they had to do this:

<Tabs>
  <TabList>
    <Tab isActive={activeTab === 0} onClick={() => setActive(0)}>First</Tab>
    <Tab isActive={activeTab === 1} onClick={() => setActive(1)}>Second</Tab>
  </TabList>
  <TabPanel isActive={activeTab === 0}>First content</TabPanel>
  <TabPanel isActive={activeTab === 1}>Second content</TabPanel>
</Tabs>

...the pattern would be useless. That's just prop drilling with extra steps.

The Real Goal

Compound components should let users write clean, declarative markup:

<Tabs>
  <TabList>
    <Tab>First</Tab>
    <Tab>Second</Tab>
  </TabList>
  <TabPanel>First content</TabPanel>
  <TabPanel>Second content</TabPanel>
</Tabs>

The components must communicate behind the scenes. The parent holds the active state, and somehow every child needs access to it—plus methods to update it—without the user explicitly passing anything down.

This is the core problem compound components solve: invisible coordination. The components share state internally while presenting a simple external API.

Key Takeaway: Compound components need shared state because their children must coordinate behavior (active states, callbacks, indices) without requiring users to manually prop-drill that coordination logic through the component tree.