Skip to the page

Component patterns · senior

Composition: children, compound components, render props and HOCs

Prefer composition (passing components and children) over configuration props; hooks replaced most HOCs and render props for logic reuse, and compound components (<Tabs><Tabs.List/><Tabs.Panel/></Tabs>) share state through context to give a flexible, accessible API.

The notes

What you must know

  • Composition first: pass children or named slots (header, footer) instead of growing a list of config props.
  • Passing children from a parent is also a performance tool: a stateful wrapper that re-renders does not re-render children it received as props (same element objects).
  • HOC (withAuth(Component)): wraps a component to add behaviour. Downsides: wrapper hell, prop name clashes, hidden props. Still fine for cross-cutting wrapping.
  • Render props (<Mouse render={(pos) => ...} />): flexible, but nesting gets deep; mostly replaced by custom hooks.
  • Custom hooks: the default way to reuse stateful logic.
  • Compound components: parent holds state in context; children (Tabs.Tab, Tabs.Panel) read it. Support controlled (value/onChange) and uncontrolled (defaultValue) modes.
  • Accessibility is part of the API: tabs need role="tablist", role="tab", aria-selected, aria-controls, role="tabpanel", arrow-key roving tabindex (WAI-ARIA APG).
  • Lazy vs keep-mounted panels: lazy saves work but loses state; keeping them mounted (or <Activity mode="hidden">) keeps state.

The red pen

Common traps: mistake → fix

  • Mistake: "Use arrays of titles and contents as props" (tabTitles, tabContents)

    Fix: prop explosion; use composition.

  • Mistake: "HOCs are deprecated"

    Fix: they still work; hooks are usually simpler.

  • Mistake: "Compound components need React.Children.map and cloneElement"

    Fix: context is the modern, more flexible way.

Easy to hard

Questions interviewers ask, easy to hard

  1. Compare HOCs, render props, custom hooks and compound components.
  2. Design the public API for a <Tabs> component used across the whole org.
  3. Support controlled and uncontrolled active tab.
  4. Lazy-mount panels or keep them mounted? Trade-offs.
  5. Add keyboard support and ARIA.

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview