Component & context patterns
Compound components, controlled vs uncontrolled, context splitting to stop provider-wide re-renders. Library-quality API design in miniature.
The mental model
A component's **props are its API**, and designing a reusable component is designing a public interface that other engineers — including future you — will call, misuse, and depend on. The whole discipline of component patterns is the discipline of API design in miniature: make the common case trivial, make the wrong case hard or impossible, and keep the *implementation* free to change without breaking every caller. The patterns in this lesson — compound components, controlled vs uncontrolled, context splitting, children composition — are the vocabulary for doing that well.
The core tension every pattern navigates: **flexibility versus a clean interface.** A component with forty boolean props (showHeader, hideFooter, variant, isCompact, withIcon, iconOnly…) is infinitely configurable and impossible to use — the "prop explosion" that turns a component into a config language. A component with no configurability is easy to call and useless for the second use case. The good patterns resolve this by moving structure and composition into how you assemble the component (its children) rather than into a flat prop bag, so the API stays small while the possibilities stay open.
The analogy that sticks: building with LEGO versus buying a pre-glued model. A pre-glued model (one giant configurable component) can only be the shape it came in — you can paint it (props) but not rearrange it. LEGO (compound components) gives you a few well-shaped pieces that snap together in orders the designer never enumerated, because the *connection* is the contract, not the final shape. <Menu>, <Menu.Trigger>, <Menu.Item> is LEGO: the consumer composes the structure, the library wires the ARIA and state through context, and neither side has to anticipate every layout.
What breaks without this model is a specific decay pattern. Components accumulate props for every new requirement until the prop list is longer than the render, state that should belong to the caller gets trapped inside, a Context provider re-renders half the app on every keystroke, and a "reusable" component becomes a maintenance liability nobody wants to touch. Each is an interface-design failure, and each has a named pattern that prevents it. Interviewers probe these because they separate people who use components from people who can design the ones a team depends on.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision