Course contentsShow
← React
Lesson 252 of 2,3968. Props PatternsFree lesson

Wrapper Components with Children

Building layout and container components that wrap arbitrary content using the children prop for maximum reusability.

Wrapper Components with Children

What You'll Learn: How to build reusable layout and container components that can wrap any content using the children prop.

The Core Idea

A wrapper component is like a picture frame — it provides structure, styling, or behavior around whatever you place inside it. By accepting children, these components become incredibly flexible containers that work with any content you pass between their opening and closing tags.

Instead of creating separate RedBox, BlueBox, and GreenBox components, you build one Box component that wraps anything:

function Box({ children, color }) {
  return (
    <div style={{ border: `2px solid ${color}`, padding: '16px' }}>
      {children}
    </div>
  );
}

// Now use it to wrap different content
<Box color="red">
  <h2>Welcome!</h2>
  <p>This is wrapped in a red box.</p>
</Box>

<Box color="blue">
  <img src="profile.jpg" alt="Profile" />
</Box>

Maximum Reusability

Wrapper components shine for layouts and containers:

function Card({ children }) {
  return (
    <div className="card-container">
      <div className="card-shadow">
        {children}
      </div>
    </div>
  );
}

function Modal({ children }) {
  return (
    <div className="modal-backdrop">
      <div className="modal-content">
        {children}
      </div>
    </div>
  );
}

The wrapper provides the structure (borders, shadows, positioning), while children makes it work with any content you need.

The Analogy

Think of wrapper components like Tupperware containers — the container provides protection and structure, but works with any food you put inside. You don't need different containers for pasta, salad, or leftovers; one container design handles them all.

Key Takeaway: Wrapper components using children let you build reusable containers that provide styling, layout, or behavior around arbitrary content, avoiding the need to create multiple specialized versions of the same structure.