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.
props.children<MyComponent> ... </MyComponent>.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 */ }
Card Component with props.childrenThe Card component doesn’t know in advance what content it will display. It simply renders props.children inside a styled container.
// Card component that renders any nested children inside a styled div
function Card(props) {
return (
<div className="card">
{props.children}
</div>
);
}
export default Card;
// 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;
On the screen, you’ll see a card-like box containing:
React Props ExampleThis content is passed as children!The Card component is reusable. You can pass any JSX as its children and it will render it.
A common pattern is to build a wrapper component that adds layout or styling, while props.children decides what content appears inside.
// 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;
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.
children with Other PropsYou 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.
// 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;
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.
You can decide when or how to render children using props and logic.
// 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;
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).
props.children<TwoColumnLayout>).children in layout or wrapper components where the structure is fixed but the inner content should be flexible.function Card({ children }) { ... }props.children can be text, JSX, or other components.children with other props (like type, title, etc.) to build powerful, reusable UI patterns.Card component that uses props.children to display any content passed inside it.Card component and render it.Panel component that accepts a title prop and children.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.