Sub topic 7
Compound components: the idea
This chapter assumes you know function components, props, useState, and passing functions as props. It also assumes you have read the earlier chapters on composition and lifting state up. Context comes in the next chapter, so here the focus is on what a compound component is and why you would build one.
The problem with configuration props
Suppose you build a tab widget as one component. You pass it a list of tabs and it renders everything.
import { useState } from 'react';
export function TabsFromConfig({ tabs }) {
const [active, setActive] = useState(tabs[0].value);
const current = tabs.find((tab) => tab.value === active);
return (
<div>
<div role="tablist">
{tabs.map((tab) => (
<button key={tab.value} role="tab" onClick={() => setActive(tab.value)}>
{tab.label}
</button>
))}
</div>
<div role="tabpanel">{current.content}</div>
</div>
);
}This works until someone needs an icon in one tab, the tab list below the panel, or a disabled tab. Each request adds another prop such as iconPosition, tabListPlacement, or disabledTabs. The component grows a large API, and each prop covers only one case.
The compound approach
A compound component is a group of components designed to be used together. A parent owns the shared state. Child components read and update that state without the consumer wiring them up. The consumer writes the structure in JSX and decides the order, layout, and extra elements.
HTML already works this way. A <select> and its <option> elements only make sense together. You don't pass <select> a list of options as a prop. You nest them, and the two coordinate behind the scenes.
Here is how a consumer uses a compound Tabs:
import { Tabs } from './Tabs';
export function Example() {
return (
<Tabs defaultValue="one">
<div role="tablist">
<Tabs.Tab value="one">One</Tabs.Tab>
<Tabs.Tab value="two">Two</Tabs.Tab>
</div>
<Tabs.Panel value="one">First panel</Tabs.Panel>
<Tabs.Panel value="two">Second panel</Tabs.Panel>
</Tabs>
);
}Neither Tabs.Tab nor Tabs.Panel receives the active value or a setter from the consumer. Each part knows only its own value. To move the tab list below the panels, the consumer reorders the JSX. To add an icon, the consumer puts one inside Tabs.Tab. The Tabs component needs no change for either.
What makes a compound component
Three properties define the pattern:
- The parent holds the state, so the pieces cannot drift out of sync.
- The children share that state implicitly. The consumer passes no state through props.
- The consumer controls the markup, order, and extra content.
Trade-offs
| Aspect | Configuration props | Compound components |
|---|---|---|
| Layout control | Fixed by the component | Chosen by the consumer |
| API surface | Grows with each case | Stays small, uses subcomponents |
| Usage | One element, easy to learn | More JSX to write |
| Misuse | Hard to misuse | Children can be used outside the parent |
Exercise
A teammate's Accordion component takes props items, iconPosition, headerClassName, and footerSlot, and requests for new props keep arriving. Which two properties of compound components would address this, and what new risk do they introduce?
Show answer
The consumer controls the markup, order, and extra content, so layout requests need no new props. The parent also holds the shared state, so the pieces stay in sync. The new risk is that subcomponents can be rendered outside their parent, where no shared state exists. This needs a guard.