What is the 'children' Prop in React?
In React, the children
prop is a special built-in prop that allows components to pass and render nested elements or components inside them.
Think of it like this: when you wrap elements between the opening and closing tags of a component, those elements become accessible inside that component through the children
prop.
Example:
function Card({ children }) { return <div className="card">{children}</div>; } function App() { return ( <Card> <h2>Hello World!</h2> <p>This is a card content.</p> </Card> ); }
In this example:
<h2>
and<p>
are passed aschildren
to theCard
component.- Inside the
Card
,{children}
will render whatever is passed between<Card>
and</Card>
.
Why is children
useful?
- It makes components more flexible and reusable.
- You can create wrapper components (like modals, cards, layouts) that can display dynamic inner content without changing the component itself.
- It encourages composition — a core concept in React for building complex UIs.
Additional Tips:
children
can be a single React element, multiple elements, strings, numbers, or even a function (in advanced patterns).- React provides utilities like
React.Children
to work withchildren
when needed (e.g., mapping or counting).