Component Composition & Compound Components
Component composition is the practice of combining small, focused React components together to build complex user interfaces. Pushing UI configuration into component children avoids 'prop drilling' (passing data through multiple intermediary components that do not need it).
The Compound Component pattern allows multiple components to share implicit state while giving callers complete freedom over rendering layout. Examples include <Select> with <Select.Option> or <Accordion> with <Accordion.Item>. Using React Context internally, parent compound components manage state while sub-components render seamlessly wherever children are placed.
Exercise
Build a compound <Tabs> component (Tabs, Tabs.List, Tabs.Tab, Tabs.Panel) using React Context.
Check your understanding
What is the primary benefit of compound components over long prop lists?Show answerHide answer
Answer
Callers can customize layout, ordering, and markup without requiring new configuration props on the parent.How do sub-components in a compound pattern share active state?Show answerHide answer
Answer
Via an internal React Context Provider rendered inside the parent component.