Sub topic 17
Choosing a pattern and its trade-offs
This chapter assumes you know function components, props, useState, context, and the patterns from earlier chapters: render props, higher-order components, custom hooks, compound components, control props, prop getters, and the state reducer. Here you will pick between them for a real problem.
Start with the problem
Each pattern solves a different reuse problem. Ask what you are sharing before you choose.
- Logic only (no markup): use a custom hook.
- Markup with a few variations: use
childrenor props as slots. - A group of parts that must coordinate (tabs, accordion, menu): use compound components with context.
- State the caller must own or override: add control props, and a state reducer if callers need to change transitions.
- A library with no styling opinions: expose a headless hook with prop getters.
Compare the costs
| Pattern | Flexibility | Main cost |
|---|---|---|
| Children and slots | Caller controls layout | No shared state |
| Render props | Caller controls output | Nested callbacks, harder to read |
| Higher-order component | Wraps existing components | Prop name clashes, hidden wrapping, awkward types |
| Custom hook | Logic reused anywhere | No markup or accessibility help |
| Compound components | Caller controls structure | Needs context and misuse guards |
| Control props | Caller owns state | Two modes to test and document |
| State reducer | Caller changes transitions | Large API surface to keep stable |
| Headless hook | Total styling freedom | Caller must wire up accessibility |
Most new code should reach for a custom hook first. Hooks replace most render prop and higher-order component uses with less nesting. Keep a render prop when the component must render something inside its own tree, such as a virtualized list row.
Add flexibility only when asked for
Every layer adds API you must support. A Switch with no control props is simpler than one with on, defaultOn, onChange, and a reducer. Add control props when a real caller needs to own the state. Add a state reducer when callers keep asking for one-off behavior changes.
The same logic can be offered at different levels. The headless hook from the earlier chapter can drive any markup:
import { useToggle } from './useToggle';
export function Mute() {
const { on, getTogglerProps } = useToggle();
return <button {...getTogglerProps()}>{on ? 'Muted' : 'Sound on'}</button>;
}A packaged Switch component is easier to use but allows less change. Offer the hook to advanced users and the component to everyone else.
Common mistakes
- Using compound components for two parts that never coordinate. Plain props are simpler.
- Passing a new object to a context provider on every render, which re-renders all consumers. Memoize the value when the tree is large.
- Building a state reducer before anyone needs one.
- Dropping the accessibility roles when going headless. The consumer must then supply them.
Exercise
A team needs a dropdown menu. Callers want to choose the markup for each item, and some callers must open the menu from outside, for example when a keyboard shortcut is pressed. Which patterns would you combine, and why?
Show answer
Use compound components with context (Menu, Menu.Button, Menu.Item) so callers control the markup while the parts share open state. Add control props (open and onOpenChange) so a caller can own the state and open the menu from a shortcut. A state reducer is not needed until callers ask to change transitions, such as keeping the menu open after a selection.