Sub topic 6
Nesting Content with the children Prop
This chapter assumes you can write function components, use JSX, and destructure props. It builds on passing props and expression values. You will learn how the children prop works and how to use it for wrapper components.
What children is
Whatever you place between a component's opening and closing tags arrives as a prop named children. React sets it for you. You do not pass it as an attribute.
function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
{children}
</section>
);
}
export default function App() {
return (
<Card title="Profile">
<p>Software engineer</p>
</Card>
);
}Card controls the frame: the section, the class, and the heading. The caller controls the content. {children} marks where that content appears. You can place it anywhere in the component, including inside other elements.
What children can contain
The value of children depends on what the caller wrote.
| Caller writes | `children` is |
|---|---|
| Nothing (`<Card />`) | `undefined` |
| Text only | A string |
| One element | A single React element |
| Several elements | An array of elements |
| An expression such as `{items.map(...)}` | Whatever the expression returns |
Because the shape varies, render children directly with {children}. Avoid indexing into it, such as children[0]. That fails when there is only one child.
Wrappers that add behavior
A wrapper can do more than add markup. It can decide whether to render its children at all.
import { useState } from 'react';
function Collapsible({ title, children }) {
const [open, setOpen] = useState(false);
return (
<div>
<button onClick={() => setOpen(!open)}>
{open ? 'Hide' : 'Show'} {title}
</button>
{open && <div>{children}</div>}
</div>
);
}
export default function App() {
return (
<Collapsible title="details">
<p>Hidden until the button is clicked.</p>
</Collapsible>
);
}Collapsible owns the open state. The content it wraps knows nothing about it. This pattern is common for modals, tooltips, layout shells, and error boundaries.
Children are created by the parent
The elements in children are created in the parent's render, not in the wrapper. When Collapsible re-renders because open changed, React receives the same children elements the parent already made. That is one reason wrappers that hold their own state are cheap to update.
It also means a child element's props come from the parent's scope. In the example above, the <p> can use any variable available in App, even though it renders inside Collapsible.
Common errors
- Forgetting
{children}. The wrapper receives the content but never renders it, so nothing appears. React shows no error. - Passing
childrenas an attribute and as nested content. If you write<Card children="a">b</Card>, the nested content wins. Use only nested content. - Assuming children is an array. A single child is not wrapped in an array, so calling
children.mapthrows. If you must iterate, useReact.Children.toArray(children), which always returns an array. Treat it as a last resort, because it changes keys and hides the real structure. - Mutating children. Like all props,
childrenis read-only. To change what is rendered, wrap or filter it instead.
Choosing between children and other props
Use children when the content is the natural body of the component, as with a button label or a card body. Use a regular prop for short values such as a title string that you may format or validate. A component can use both, as Card does.
Exercise
A component Panel is written as function Panel({ heading }) { return <div><h2>{heading}</h2></div>; }. A caller writes <Panel heading="Notes"><p>Buy milk</p></Panel>. Why does "Buy milk" not appear, and how do you fix it?
Show answer
The <p> is passed to Panel as the children prop, but Panel does not read or render it. React gives no error. Destructure children in the parameter list ({ heading, children }) and render {children} inside the div, after the h2.
Exercise
What does children contain when a caller writes <List>{items.map(i => <li key={i}>{i}</li>)}</List> with three items, and why is children.length unsafe in general?
Show answer
It contains an array of three li elements, because the expression returns an array. children.length is unsafe because children can be undefined, a string, or a single element when the caller writes different content, and those values do not have the length you expect. Render {children} directly, or use React.Children.count(children) if you need a count.