What problem does it solve?
The compound component pattern solves the problem of building related UI components (like toggles and lists) that must share implicit state without forcing consumers to manually pass props through multiple layers.
Core Features & Use Cases
- Shared implicit state via Context: Use React Context to coordinate open/close or selection state across parent and child components.
- Ergonomic component API with static properties: Attach sub-components as properties on the parent (e.g., Parent.Toggle, Parent.List) so consumers import only the parent.
- More flexible nesting than direct cloning-only approaches: Prefer Context-based coordination to enable nested or wrapped compositions without losing access to state.
Quick Start
Use the compound component pattern in React by creating a parent component that holds the shared state in a Context provider and exposing coordinated sub-components as static properties (for example, Parent.Toggle and Parent.List) for a dropdown, tabs, accordion, or menu UI.