--- name: composing-components description: Teaches component composition patterns in React 19 including children prop, compound components, and render props. Use when designing component APIs, creating reusable components, or avoiding prop drilling. allowed-tools: Read, Write, Edit version: 1.0.0 --- # Component Composition Patterns This skill teaches you how to compose components effectively using React 19 patterns. This skill activates when: - Designing reusable component APIs - Need to avoid prop drilling - Creating compound components (Tabs, Accordion, etc.) - Building flexible, composable interfaces - Choosing between composition patterns React 19 supports multiple composition patterns: 1. **Children Prop** - Simplest composition, pass components as children 2. **Compound Components** - Components that work together (Context-based) 3. **Render Props** - Functions as children for flexibility 4. **Composition over Props** - Prefer slots over configuration props **When to Use:** - Children prop: Simple containment - Compound components: Coordinated behavior (tabs, accordions) - Render props: Custom rendering logic - Slots: Multiple insertion points ## Pattern 1: Children Prop ```javascript function Card({ children, header, footer }) { return (
{header &&
{header}
}
{children}
{footer &&
{footer}
}
); } Title} footer={}>

Card content goes here

; ``` ## Pattern 2: Compound Components ```javascript import { createContext, use } from 'react'; const TabsContext = createContext(null); export function Tabs({ children, defaultTab }) { const [activeTab, setActiveTab] = useState(defaultTab); return (
{children}
); } export function TabList({ children }) { return
{children}
; } export function Tab({ id, children }) { const { activeTab, setActiveTab } = use(TabsContext); return ( ); } export function TabPanel({ id, children }) { const { activeTab } = use(TabsContext); if (activeTab !== id) return null; return
{children}
; } ``` Usage: ```javascript Profile Settings ``` ## Pattern 3: Render Props ```javascript function DataProvider({ render, endpoint }) { const [data, setData] = useState(null); useEffect(() => { fetch(endpoint) .then((res) => res.json()) .then(setData); }, [endpoint]); return render({ data, loading: !data }); } (loading ? : )} />; ```
## Example: Modal with Composition ```javascript function Modal({ children, isOpen, onClose }) { if (!isOpen) return null; return (
e.stopPropagation()}> {children}
); } function ModalHeader({ children }) { return
{children}
; } function ModalBody({ children }) { return
{children}
; } function ModalFooter({ children }) { return
{children}
; } Modal.Header = ModalHeader; Modal.Body = ModalBody; Modal.Footer = ModalFooter; ``` Usage: ```javascript setShowModal(false)}>

Confirm Action

Are you sure you want to proceed?

``` For comprehensive composition patterns, see: `research/react-19-comprehensive.md` lines 1263-1293.
## MUST - Use `use()` API for Context in React 19 (can be conditional) - Keep compound components cohesive - Document required child components ## SHOULD - Prefer composition over complex prop APIs - Use Context for compound component state - Provide good component names for debugging ## NEVER - Over-engineer simple components - Use Context for non-coordinated components - Forget to handle edge cases (missing children, etc.)