Sub topic 8
Nesting content with children
This chapter assumes you can write a function component, pass props to it, and read basic JSX with curly braces. It covers how a component receives the content placed between its tags, and the mistakes that most often go with it.
Content between the tags
Props carry data in through attributes. Sometimes you want to pass markup instead. React handles this with a special prop named children. Whatever you write between a component's opening and closing tags arrives in that prop.
function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
{children}
</section>
);
}
function App() {
return (
<Card title="Welcome">
<p>This paragraph is passed as children.</p>
</Card>
);
}Card controls the frame: the section, the heading, and the class. App controls the content inside it. Card knows nothing about what it wraps, so you can reuse it with text, a form, or another Card.
Children is an ordinary prop
Nothing about children is special at runtime. The JSX compiler gathers nested content and passes it in the props object. This is the automatic runtime output for the example above, with Card also written as plain calls:
import { jsx as _jsx, jsxs as _jsxs } from 'react/jsx-runtime';
function Card({ title, children }) {
return _jsxs('section', {
className: 'card',
children: [_jsx('h2', { children: title }), children],
});
}
const element = _jsx(Card, {
title: 'Welcome',
children: _jsx('p', { children: 'This paragraph is passed as children.' }),
});This also explains the shape of the value. One child arrives as a single value. Several children arrive as an array. Text arrives as a string. If there are none, children is undefined.
Where you put it matters
The component decides where children appear. It can place them anywhere in its output, and it can render them more than once, or not at all.
function Panel({ children }) {
return (
<div className="panel">
<header>Panel</header>
<div className="panel-body">{children}</div>
<footer>End of panel</footer>
</div>
);
}For several distinct slots, such as a header area and a footer area, pass elements through named props like header={<h2>Hi</h2>} and keep children for the main content.
Common errors
- Forgetting
{children}. If the component never renders the prop, nested content is silently dropped. No error appears. - Writing
childrenas text. Typingchildreninside JSX prints the word. Use{children}. - Using a self-closing tag by mistake.
<Card title="Hi" />passes no children, sochildrenisundefined. Render nothing extra, or guard for it with{children && ...}if the wrapper should disappear. - Assuming
childrenis an array. Callingchildren.mapfails when there is one child or none. If you need to iterate, useReact.Children.map, but most designs avoid needing it. - Passing both ways. If you write
<Card children="a">b</Card>, the nestedbwins. Pick one style.
Exercise
A Box component is defined as function Box({ label, children }) { return <div><strong>{label}</strong></div>; }. You render <Box label="Note"><p>Remember this.</p></Box>, but the paragraph never appears. What is wrong, and what is the fix?
Show answer
Box never renders its children prop, so the nested paragraph is discarded without an error. Add {children} to the output, for example <div><strong>{label}</strong>{children}</div>.