Skip to main content

Advanced Composition Patterns

Patterns are tools for recurring design problems. Use them only when they improve ownership, composition, reuse, testability, API clarity, or isolation of complexity.

Prefer plain composition firstโ€‹

function Panel({ title, children }) {
return (
<section>
<h2>{title}</h2>
{children}
</section>
);
}
Do you need a more advanced pattern?
Children + props express the API clearlyKeep ordinary composition
Several children coordinate one interaction modelConsider compound components
Consumers need reusable stateful behaviorConsider a custom Hook
Consumers need to customize transitionsConsider reducer/state-machine patterns
A vendor/runtime contract should be isolatedUse an adapter boundary

Compound componentsโ€‹

<Menu>
<Menu.Trigger>Account</Menu.Trigger>
<Menu.Content>
<Menu.Item>Profile</Menu.Item>
<Menu.Item>Log out</Menu.Item>
</Menu.Content>
</Menu>
Compound components coordinate implicit shared behavior
Menu root
owns open state + public contract
Trigger
activation + aria state
Content
presence + focus scope
Items
selection + keyboard navigation

Keep internal coordination private when consumers do not need it.

Controlled and uncontrolled APIsโ€‹

function Toggle({ pressed, defaultPressed = false, onPressedChange }) {
const [internal, setInternal] = useState(defaultPressed);
const isControlled = pressed !== undefined;
const value = isControlled ? pressed : internal;

function setValue(next) {
if (!isControlled) setInternal(next);
onPressedChange?.(next);
}

return <button aria-pressed={value} onClick={() => setValue(!value)} />;
}
Ownership should stay stable for the component instance
Controlled
Parent owns value
Uncontrolled
Component owns value

Switching between the two modes during the same lifetime is usually a bug.

Reducers model explicit transitionsโ€‹

type State =
| { status: 'closed' }
| { status: 'opening' }
| { status: 'open' }
| { status: 'closing' };

type Action =
| { type: 'OPEN' }
| { type: 'OPENED' }
| { type: 'CLOSE' }
| { type: 'CLOSED' };
State machine thinking prevents contradictory booleans
1closed
2opening
3open
4closing

The value of a reducer is not that it stores several fields; it is that events and transitions become explicit.

State reducer patternโ€‹

Some reusable libraries allow consumers to intercept a proposed transition:

<Toggle
stateReducer={(state, action, nextState) => {
if (action.type === 'toggle' && state.locked) return state;
return nextState;
}}
/>
State reducer extension point
Internal event
toggle
Next state
default transition
Accepted state
customized result

This is powerful but creates a much larger public API surface. Use it only when transition customization is a real requirement.

Hooks vs render propsโ€‹

What is being reused?
Stateful behavior without owning render structureCustom Hook
A component owns lifecycle/structure and caller renders a regionRender prop can fit
Only static structure differsChildren/composition is usually simpler
const { x, y } = useMousePosition();
<DataLoader>
{({ data, pending, error }) => {
if (pending) return <Spinner />;
if (error) return <ErrorView />;
return <Chart data={data} />;
}}
</DataLoader>

Hooks replaced many historical render-prop use cases, but render props are still valid when render structure itself is part of the API.

Higher-order componentsโ€‹

const Enhanced = withPermissions(Component);

HOCs still matter in older codebases and some library/framework APIs. For new application architecture, Hooks and composition are often easier to type, debug, and compose.

Adapter components isolate vendor APIsโ€‹

function ProductChart({ points }) {
return (
<ThirdPartyChart
series={[{ data: points }]}
animation={false}
/>
);
}
Keep vendor details behind an application-owned contract
Feature
domain language
App adapter
stable contract
Vendor SDK
third-party API

The same pattern works for analytics, payments, maps, editors, and provider components.

Headless behavior + styled presentationโ€‹

Separate interaction rules from visual presentation when reuse demands it
Headless behavior
state ยท keyboard ยท focus ยท ARIA ยท events
Presentation A
product design
Presentation B
another theme/product

Do not split behavior and presentation mechanically. Separation is valuable when multiple presentations genuinely share the same interaction contract.

Pattern selection checklistโ€‹

What problem are you solving?
Coordinate related child primitivesCompound components + private Context
Offer parent-owned or self-owned valueControlled/uncontrolled API
Model complex transitionsReducer/state machine
Reuse stateful behaviorCustom Hook
Isolate third-party implementationAdapter component/provider
No recurring problem yetUse plain React composition

Architectural ruleโ€‹

1Start explicit
2Observe repetition
3Name the shared concept
4Choose the smallest pattern
5Test the public contract

A pattern earns its complexity when it makes ownership and change easier to reason about than the simpler code it replaces.