Wrapper Components for Shared Behavior
What you'll learn: How to create reusable components that wrap other components to add common functionality or styling.
The Core Idea
A wrapper component is a component whose primary job is to "wrap around" other components and add something extra — like styling, error handling, or common layout structure. Think of it like a picture frame: the frame doesn't change what the picture is, but it adds presentation and context around it.
Instead of repeating the same surrounding logic in multiple places, you create one wrapper component that handles it once.
Why This Matters
Imagine you want every section of your app to have a consistent card-style container with padding and a border. Without wrappers, you'd repeat that styling everywhere. With a wrapper, you write it once and reuse it.
The Basic Pattern
The wrapper component takes children as a prop and renders them inside whatever structure you need:
function Card({ children }) {
return (
<div className="card-container">
{children}
</div>
);
}
Now you can use it anywhere:
function App() {
return (
<Card>
<h2>Profile</h2>
<p>User information here</p>
</Card>
);
}
The Card wrapper adds the container structure, while the actual content comes from whatever you put between its opening and closing tags.
This is composition in action — you're building complex UIs by combining simpler pieces. Wrappers handle shared behavior once, making your code more maintainable and consistent.
Key Takeaway: Wrapper components accept children and wrap them with shared functionality, styling, or structure, eliminating repetition and centralizing common patterns.