Spreading with Component Composition
What you'll learn: How to combine prop spreading with composition patterns to create powerful, flexible wrapper components that enhance behavior without obscuring the underlying component's API.
The Core Idea
You've learned that spreading props ({...props}) lets wrapper components pass through unknown props, and you've learned that composition lets you build complex UIs from simpler pieces. When you combine these two patterns, you get something special: wrapper components that can enhance or modify behavior while remaining completely transparent to the underlying API.
Think of it like a universal adapter for electronics. It adds functionality (like surge protection or USB ports) but doesn't block the original plug—everything still works exactly as expected, just with extra features layered on top.
How It Works
Instead of wrapping a native element with spread props, you wrap a component while spreading:
function EnhancedButton(props) {
return (
<Button
{...props}
className={`enhanced ${props.className || ''}`}
/>
);
}
Now EnhancedButton accepts everything Button accepts, plus adds its own enhancement (a CSS class). Users don't need to learn a new API—they already know how to use Button, and this just works.
Why This Matters
This pattern lets you:
- Add analytics, logging, or tracking to existing components
- Apply consistent styling without creating new prop interfaces
- Wrap third-party components with custom behavior
- Keep your component library flexible and extensible
The key is that spreading with composition preserves the full API of the wrapped component, making your wrappers feel invisible while still providing value.
Key Takeaway: Spreading props onto composed components creates transparent wrappers that enhance functionality without forcing users to learn new APIs—the wrapped component's interface shines through naturally.