--- name: react-composition-2026 description: Teaches modern React composition patterns for 2025/2026. Use when designing component APIs, building shared UI libraries, or refactoring prop-heavy components. context: fork allowed-tools: Read, Grep, Glob paths: - "**/*.tsx" - "**/*.jsx" license: MIT metadata: author: patterns.dev version: "1.1" related_skills: - "hooks-pattern" - "hoc-pattern" --- # Modern React Composition Patterns ## Table of Contents - [When to Use](#when-to-use) - [Instructions](#instructions) - [Details](#details) - [Source](#source) Composition patterns for building flexible, maintainable React components that scale. These patterns replace boolean-prop proliferation, rigid component APIs, and tangled state with composable, explicit designs. ## When to Use Reference these patterns when: - A component has more than 3-4 boolean props controlling its behavior - Building reusable UI components or a shared component library - Refactoring components that are difficult to extend - Designing component APIs that other teams will consume - Reviewing component architecture for flexibility and maintainability ## Instructions - Apply these patterns during component design, code generation, and review. When you see boolean prop accumulation or rigid component APIs, suggest the appropriate composition pattern. ## Details ### Overview The core principle: **composition over configuration**. Instead of adding boolean props and conditional branches to handle every variant, compose smaller, focused components together. This makes components easier to understand, test, and extend — for both humans and AI agents. --- ### 1. Replace Boolean Props with Composition **Impact: HIGH** — Prevents combinatorial explosion and makes intent explicit. Boolean props multiply complexity: 4 booleans = 16 possible states, most of which are untested. Replace them with composable children. **Avoid — boolean prop accumulation:** ```tsx ``` **Prefer — explicit composition:** ```tsx

Title

Content here

``` Each piece is explicit, testable, and independently optional. --- ### 2. Build Compound Components with Context **Impact: HIGH** — Shared implicit state without prop drilling. Compound components are a group of components that work together, sharing state through context rather than props. The parent owns the state; children consume it. **Avoid — parent manages everything through props:** ```tsx Pick a color Red Blue ``` --- ### 3. Create Explicit Variant Components **Impact: MEDIUM** — Makes each mode a clear, focused component. When a component has distinct "modes" (dialog vs drawer, inline vs modal, card vs list-item), create explicit variant components instead of toggling with props. **Avoid — one component with mode props:** ```tsx function MediaDisplay({ type, src, title, showControls, autoPlay, loop }: Props) { if (type === 'video') { return