The children Prop
The children prop holds content placed between a component's tags. The content can be text or elements.
Pass children
The paragraph sits between the Card tags.
<Card>
<p>Read this first.</p>
</Card>Render children
props.children holds the inner content.
function Card(props) {
return <div>{props.children}</div>
}Many children
Two elements sit inside Card.
<Card>
<h2>Note</h2>
<p>Read this first.</p>
</Card>Example
function Card(props) {
return <div>{props.children}</div>
}
export default function App() {
return (
<Card>
<h2>Note</h2>
<p>Read this first.</p>
</Card>
)
}
Key points
- children is a special prop name in React.
- It holds whatever sits between the opening and closing tags.
- The component renders children with {props.children}.
- children can hold one element or many elements.
In this example, Card takes props as its first parameter. Card returns a div with props.children inside. App places a heading and a paragraph inside Card. The page shows both elements inside the div.
Note
children needs a closing tag. A self-closing tag has no children.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Where does the content between Card tags go?