← Back to Chapters

Children (props.children)

⚛️ Children (props.children)

⚡ Quick Overview

In React, props.children is a special prop that every component receives by default. It represents whatever JSX/content is placed between the opening and closing tags of that component.

This makes components more flexible and reusable, allowing them to wrap and display any nested elements passed inside.

? Key Concepts of props.children

  • Special prop: Automatically passed to every component by React.
  • Holds nested content: Everything between <MyComponent> ... </MyComponent>.
  • Perfect for wrappers: Create reusable containers like cards, panels, or layouts.
  • Can be anything: Text, elements, or other components (React nodes).

? Syntax & Theory

When you write a component like:

<Card>Some content here</Card>

Inside the Card component, that “Some content here” value is available as props.children (or children if you destructure the props).

function Card({ children }) { /* use children here */ }

? Example 1: Basic Card Component with props.children

The Card component doesn’t know in advance what content it will display. It simply renders props.children inside a styled container.

? View Code Example (Card.js)
// Card component that renders any nested children inside a styled div
function Card(props) {
return (
<div className="card">
{props.children}
</div>
);
}

export default Card;
? View Code Example (App.js)
// App component passing children content into the Card component
import Card from "./Card";

function App() {
return (
<div>
<Card>
<h3>React Props Example</h3>
<p>This content is passed as children!</p>
</Card>
</div>
);
}

export default App;

?️ What will this render?

On the screen, you’ll see a card-like box containing:

  • A heading: React Props Example
  • A paragraph: This content is passed as children!

The Card component is reusable. You can pass any JSX as its children and it will render it.

? Example 2: Wrapper Component

A common pattern is to build a wrapper component that adds layout or styling, while props.children decides what content appears inside.

? View Code Example (Wrapper.js + usage)
// Wrapper component that adds a border and padding around its children
function Wrapper({ children }) {
return (
<div style={{ border: "2px solid gray", padding: "10px" }}>
{children}
</div>
);
}

// Using the Wrapper component with nested content as children
function App() {
return (
<Wrapper>
<h2>Inside Wrapper</h2>
<p>This is wrapped content.</p>
</Wrapper>
);
}

export default App;

? Output Explanation

The Wrapper component behaves like a styled box. Any elements placed between <Wrapper> and </Wrapper> appear inside that box, sharing the same border and padding.

? Example 3: Combining children with Other Props

You can combine props.children with other props to create flexible, dynamic components. Here’s an AlertBox that uses a type prop to decide its style while using children for the message.

? View Code Example (AlertBox)
// AlertBox component that styles itself based on type and shows children as the message
function AlertBox({ type, children }) {
const colors = {
success: "#d4edda",
warning: "#fff3cd",
error: "#f8d7da"
};

return (
<div style={{
backgroundColor: colors[type],
padding: "10px",
borderRadius: "5px"
}}>
{children}
</div>
);
}

// Usage examples of the AlertBox component with different types
function App() {
return (
<div>
<AlertBox type="success">Form submitted successfully!</AlertBox>
<AlertBox type="error">Error: Invalid credentials!</AlertBox>
</div>
);
}

export default App;

? Output Explanation

Depending on the type prop:

  • success → green-like background with a success message.
  • error → red-like background with an error message.

The style comes from the type prop, while the text comes from children.

? Example 4: Conditional Rendering of Children

You can decide when or how to render children using props and logic.

? View Code Example (ConditionalWrapper)
// ConditionalWrapper shows its children only when show is true
function ConditionalWrapper({ show, children }) {
return <div>{show ? children : "Hidden Content"}</div>;
}

// Usage example that conditionally displays paragraph content
function App() {
return (
<ConditionalWrapper show={true}>
<p>This will be visible!</p>
</ConditionalWrapper>
);
}

export default App;

? Output Explanation

If show is true, the nested children are rendered. If show is false, the text "Hidden Content" appears instead.

This pattern is powerful for toggling content visibility (e.g. accordions, modals, feature flags).

? Common Use Cases for props.children

  • Reusable layout components like cards, panels, and sections.
  • Modals and dialogs that wrap arbitrary content.
  • Custom layout components (e.g., <TwoColumnLayout>).
  • Provider-like components that add context or styling around children.

? Tips & Best Practices

  • Use children in layout or wrapper components where the structure is fixed but the inner content should be flexible.
  • Destructure props for cleaner code: function Card({ children }) { ... }
  • Remember that props.children can be text, JSX, or other components.
  • Combine children with other props (like type, title, etc.) to build powerful, reusable UI patterns.

? Try It Yourself

  1. Create a Card component that uses props.children to display any content passed inside it.
  2. Wrap multiple elements (heading, paragraph, button) inside the Card component and render it.
  3. Build a Panel component that accepts a title prop and children.
  4. Use the Panel component to render panels with different headings but shared styling.

Goal: Get comfortable using props.children to build flexible wrapper components that can hold any nested content dynamically.