React Design and Engineering Patterns
Component API design
Design component props for clarity and extension. Avoid "prop explosion"—dozens of boolean flags like isCompact, hasShadow, showIcon, and hideBorder that create conflicting combinations. Instead, use explicit variants (size="sm" | "md") and composition slots (children, header, actions):
function Panel({ title, actions, children }) {
// Composition slots avoid rigid boolean flags for every header variation
return (
<section className="card p-3">
<header className="d-flex justify-content-between align-items-center mb-2">
<h3 className="h5 mb-0">{title}</h3>
{actions && <div>{actions}</div>}
</header>
<div>{children}</div>
</section>
);
}
Reuse patterns
Choose the lightest reuse mechanism that fits the problem:
- Composition with
childrenand slots: Share layout shells and visual framing without coupling to inner content. - Custom hooks (
usePagination,useDebounce): Share stateful behavior and side-effect orchestration across unrelated UI components. - Compound components: Pair a parent context with subcomponents (such as
Tabs,Tabs.List,Tabs.Panel) when child elements need implicit shared state.
Testing design
Write tests that exercise what users actually see and do—querying by accessible role, label, or visible text—rather than asserting on internal hook state or CSS class names. Tests coupled to user behavior survive refactoring, whereas tests coupled to implementation details break whenever you rename a helper:
// Prefer behavior-oriented queries (e.g., React Testing Library)
// render(<Counter initial={0} />);
// await user.click(screen.getByRole("button", { name: /increment/i }));
// expect(screen.getByText("Count: 1")).toBeInTheDocument();
Performance patterns
Measure with the React DevTools Profiler before optimizing. When profiling confirms a genuine bottleneck, first fix structural causes—such as moving state down into a smaller child component or passing children so parent re-renders skip unchanged subtrees—before reaching for React.memo, useMemo, or useCallback.
Engineering checklist
Review new components and features against these four quality gates:
- Clear, minimal prop contracts with sensible defaults.
- Keyboard-accessible interactions and semantic HTML elements.
- Deterministic side effects with proper cleanup and error handling.
- Behavior-focused tests covering happy paths, empty states, and failures.
Practice
Design a reusable DataTable component using composition slots for toolbar actions and empty states, extract its sorting/pagination state into a custom hook, and write behavior tests for sorting and empty results.