ReactHard
What are Compound Components?
Compound components are a set of components that work together and share implicit state, giving users a flexible, declarative API, like <select> and <option> in HTML. For example <Tabs>, <Tabs.List>, <Tabs.Tab> and <Tabs.Panel>: the parent holds the state and children read it through context, so users compose the parts in any layout without wiring props by hand.
Why use them
- Flexible markup: users choose the order, wrapping elements and styling of each part.
- Clean API: no giant configuration objects or dozens of props.
- Encapsulated state: the parent manages selection, open state or focus.
- Used by Radix UI, Headless UI, React Aria, Reach UI and shadcn/ui.
Implementation
- Create a context for the shared state and actions.
- The root component provides it (controlled or uncontrolled:
value/defaultValue,onValueChange). - Child components read the context and throw a helpful error if rendered outside the root.
- Attach children as static properties (
Tabs.Tab) or export them separately (better for tree shaking and Server Components). - Handle accessibility: roles (
tablist,tab,tabpanel),aria-selected,aria-controls, keyboard navigation, and IDs fromuseId.
Older approach
React.Children.map + cloneElement to inject props into direct children. Fragile with wrappers and fragments; context is the modern approach.
Pitfalls
- Over-flexibility can produce invalid structures; validate in development.
- Keep context values memoized to avoid re-rendering every part.
Code example
tsx
1import { createContext, use, useId, useState } from "react";23type TabsCtx = { value: string; setValue: (v: string) => void; baseId: string };4const TabsContext = createContext<TabsCtx | null>(null);56function useTabs() {7 const ctx = use(TabsContext);8 if (!ctx) throw new Error("Tabs parts must be used inside <Tabs>");9 return ctx;10}1112export function Tabs({ defaultValue, children }: { defaultValue: string; children: React.ReactNode }) {13 const [value, setValue] = useState(defaultValue);14 const baseId = useId();15 return <TabsContext value={{ value, setValue, baseId }}><div>{children}</div></TabsContext>;16}1718export function TabList({ children }: { children: React.ReactNode }) {19 return <div role="tablist">{children}</div>;20}2122export function Tab({ value, children }: { value: string; children: React.ReactNode }) {23 const t = useTabs();24 return (25 <button role="tab" id={`${t.baseId}-tab-${value}`} aria-selected={t.value === value}26 aria-controls={`${t.baseId}-panel-${value}`} onClick={() => t.setValue(value)}>27 {children}28 </button>29 );30}3132export function TabPanel({ value, children }: { value: string; children: React.ReactNode }) {33 const t = useTabs();34 if (t.value !== value) return null;35 return <div role="tabpanel" id={`${t.baseId}-panel-${value}`} aria-labelledby={`${t.baseId}-tab-${value}`}>{children}</div>;36}3738// <Tabs defaultValue="a"><TabList><Tab value="a">Account</Tab><Tab value="b">Billing</Tab></TabList>39// <TabPanel value="a">Account settings</TabPanel><TabPanel value="b">Invoices</TabPanel></Tabs>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.