Component Composition
On this page
Composition in React
- Instead of adding more flags to one component, combine smaller components into a feature.
- Composition keeps APIs stable under change.
- It improves testability by making structure explicit.
Configuration vs Composition
- Configuration: one component with many props controlling variants.
- Composition: smaller parts assembled in readable page structure.
Primitive and Wrapper Pattern
- Primitives: Button, Input, Stack.
- Wrappers: feature orchestration (UserSearchPanel, BillingForm).
- Keep business logic out of primitives.
Slots with children
function Card({ title, actions, children }) {
return (
<section>
<div>
<h3>{title}</h3>
<div>{actions}</div>
</div>
<div>{children}</div>
</section>
);
}
Example: Composed Feature
function Page() {
return (
<Card title="Users" actions={<button>Add</button>}>
<UserFilters />
<UserTable />
</Card>
);
}
Failure Modes
- Prop explosion from boolean matrices.
- Hidden coupling from unrelated prop combinations.
- Over abstracted primitives that contain business logic.
Code Review Checklist
- Reject new boolean flags unless justified.
- Prefer slots and structure over configuration.
- Primitives stay generic; features own orchestration.