Full answer
A single component with many props, such as <Tabs tabs={[...]} panels={[...]} renderTab={...} />, grows into a prop explosion. Every new layout need becomes another prop. Compound components turn the structure over to the consumer:
<Tabs defaultValue="marks"><Tabs.List><Tabs.Tab value="marks">Marks</Tabs.Tab></Tabs.List><Tabs.Panel value="marks">...</Tabs.Panel></Tabs>
The parent owns the state (which tab is active) and shares it through a context. The children read it with a hook. Because the parts are ordinary components, the consumer can wrap them, reorder them, add icons or put extra markup between them.
Design decisions that make it good. Throw a clear error if a part is used outside its parent, by checking the context. Support both controlled (value and onValueChange) and uncontrolled (defaultValue) use. Keep each part small and give it an accessible role: tablist, tab with aria-selected, tabpanel, plus keyboard navigation with arrow keys. Spread remaining props onto the underlying element so consumers can pass className and data attributes. Attach the parts as properties of the parent for discoverability (Tabs.Tab = Tab), or export them separately for better tree shaking.
Alternatives. A render-prop or a headless hook (useTabs) gives the logic without markup. Libraries such as Radix UI and React Aria use the compound approach for this reason. The trade-off is that the consumer can compose invalid combinations, so validate with the context check.
Context re-renders every part when the active tab changes, which is fine here because the set is small.
The example is a compact Tabs with an uncontrolled default and clear error handling.
import { createContext, useContext, useState } from 'react';
const TabsContext = createContext(null);
function useTabs() {
const context = useContext(TabsContext);
if (context === null) {
throw new Error('Tabs parts must be used inside <Tabs>');
}
return context;
}
function Tabs({ defaultValue, children }) {
const [active, setActive] = useState(defaultValue);
return <TabsContext value={{ active, setActive }}>{children}</TabsContext>;
}
function TabList({ children }) {
return <div role="tablist">{children}</div>;
}
function Tab({ value, children }) {
const { active, setActive } = useTabs();
return (
<button role="tab" aria-selected={active === value} onClick={() => setActive(value)}>
{children}
</button>
);
}
function TabPanel({ value, children }) {
const { active } = useTabs();
return active === value ? <div role="tabpanel">{children}</div> : null;
}
Tabs.List = TabList;
Tabs.Tab = Tab;
Tabs.Panel = TabPanel;
export default function App() {
return (
<Tabs defaultValue="marks">
<Tabs.List>
<Tabs.Tab value="marks">Marks</Tabs.Tab>
<Tabs.Tab value="attendance">Attendance</Tabs.Tab>
</Tabs.List>
<Tabs.Panel value="marks">Marks: 82 out of 100</Tabs.Panel>
<Tabs.Panel value="attendance">Attendance: 91 percent</Tabs.Panel>
</Tabs>
);
}Tabs holds the active value. Each part reads it from context and the consumer controls the structure. The check pressed each tab button once, so the last tab pressed is active. Rendered result: <div role="tablist"><button role="tab" aria-selected="false">Marks</button><button role="tab" aria-selected="true">Attendance</button></div><div role="tabpanel">Attendance: 91 percent</div>
Key points
- Compound components share implicit state through context and let the consumer compose the parts.
- They replace configuration-heavy props with flexible JSX structure.
- Support controlled and uncontrolled use, and throw a clear error when a part is used outside its parent.
- Add ARIA roles and keyboard handling for accessibility.
- Headless hooks and libraries such as Radix and React Aria apply the same idea.
Common mistakes
- Building the pattern with React.Children and cloneElement, which breaks when a consumer wraps a part in another component.
- Forgetting to support controlled use.
- No error when a part is used outside its parent.
- Ignoring accessibility roles and keyboard support.
- Creating a new context value object on every render of a large component set without considering the cost.
Follow-ups
- Why is context better than cloneElement for this pattern?
- How would you add keyboard navigation with arrow keys to the tab list?
- When would you prefer a headless hook over compound components?
Tips
- Show the target JSX first. It makes the design goal clear.
- Mention controlled and uncontrolled support. It signals library-level thinking.
- Reference real libraries (Radix, React Aria) as proof the pattern is established.