Whenever a component is used with content nested between its tags, like <Card><p>Bio</p></Card>, React automatically passes that nested content to the component as a special prop called children — the component can then place props.children anywhere inside its own returned JSX to render that nested content wherever it belongs, most commonly to build generic, reusable wrapper components like a Card, Modal, or Layout that don't need to know anything about the specific content they'll eventually wrap. children can be a single element, a string, multiple elements, or even a function, depending on what was actually nested.
1Understanding Children
Whenever a component is used with content nested between its tags, like <Card><p>Bio</p></Card>, React automatically passes that nested content to the component as a special prop called children — the component can then place props.children anywhere inside its own returned JSX to render that nested content wherever it belongs, most commonly to build generic, reusable wrapper components like a Card, Modal, or Layout that don't need to know anything about the specific content they'll eventually wrap. children can be a single element, a string, multiple elements, or even a function, depending on what was actually nested.
Use props.children to build genuinely generic wrapper components, like a Card or Modal, that render a consistent container/style but accept completely arbitrary nested content, rather than hardcoding a specific content prop shape into the wrapper itself.
function Card({ children }) {
return <div className="card">{children}</div>;
}
function App() {
return (
<Card>
<h2>Title</h2>
<p>Some content here.</p>
</Card>
);
}2Practical Example
Here is a real-world application of Children showing how it is used in production React code.
function Modal({ children, isOpen }) {
if (!isOpen) return null;
return <div className="modal-overlay"><div className="modal-content">{children}</div></div>;
}
// <Modal isOpen={true}><p>Are you sure?</p></Modal>3Best Practices
Follow these guidelines when working with Children:
1. Use props.children for wrapper components, like Card, Modal, or Layout, that need to render whatever arbitrary content is nested inside them
2. Provide a fallback or default value for props.children when a component might be used with nothing nested inside it, avoiding a rendering error or blank output
3. Use React.Children utilities, or simply array methods if children is guaranteed to be an array, when a wrapper component needs to inspect, count, or transform its children rather than just render them as-is
Tip: Use props.children to build genuinely generic wrapper components, like a Card or Modal, that render a consistent container/style but accept completely arbitrary nested content, rather than hardcoding a specific content prop shape into the wrapper itself.
function Card({ children }) {
return <div className="card">{children}</div>;
}
function App() {
return (
<Card>
<h2>Title</h2>
<p>Some content here.</p>
</Card>
);
}