Children and composition
Overview
HTML elements nest naturally: a <ul> contains <li>s, a <button> contains text or an icon. Your components can work the same way. The children prop lets a component wrap content it knows nothing about — the key to building flexible, reusable UI.
The children prop
Whatever you put between a component’s tags is passed to it as children:
<Card title="Hooks">
<p>Learn useState and useEffect.</p>
<Button>Start</Button>
</Card>function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
{children}
</section>
);
}Card doesn’t know or care whether its children are a paragraph, a form or a chart. It provides the frame; the parent decides what goes in it. children can be text, elements, an array of elements, or nothing at all.
Common wrapper components
Once you recognise the pattern, you’ll see wrapper components everywhere:
function Button({ variant = 'primary', children, ...rest }) {
return (
<button className={`btn btn-${variant}`} {...rest}>
{children}
</button>
);
}
<Button onClick={save}>Save course</Button>
<Button variant="ghost" onClick={cancel}>
<XIcon /> Cancel
</Button>Here ...rest collects every other prop (onClick, type, disabled, aria-label…) and forwards it to the real <button>, so Button supports everything a button does without listing each prop.
Layouts are wrappers too:
function PageLayout({ children }) {
return (
<div className="page">
<SiteHeader />
<main className="page-content">{children}</main>
<SiteFooter />
</div>
);
}Multiple slots
children is one slot. For more, pass JSX through other props — they’re just values:
function Panel({ title, actions, footer, children }) {
return (
<section className="panel">
<header>
<h2>{title}</h2>
{actions}
</header>
<div className="panel-body">{children}</div>
{footer && <footer>{footer}</footer>}
</section>
);
}
<Panel
title="Lessons"
actions={<Button variant="ghost">Edit</Button>}
footer={<p>49 lessons · 12 hours</p>}
>
<LessonList />
</Panel>This keeps Panel generic: it controls the layout and styling of the header, body and footer, while the parent supplies the content.
Composition instead of configuration
A common anti-pattern is a component that grows a prop for every variation:
// ✗ Configuration: every new need adds a prop
<Dialog
title="Delete course?"
message="This cannot be undone."
showIcon
iconType="warning"
confirmText="Delete"
confirmVariant="danger"
showCancel
/>With composition, you build specific components from small generic ones:
// ✓ Composition: small pieces, combined
function DeleteCourseDialog({ onConfirm, onCancel }) {
return (
<Dialog>
<Dialog.Title>
<WarningIcon /> Delete course?
</Dialog.Title>
<p>This cannot be undone.</p>
<Dialog.Actions>
<Button variant="ghost" onClick={onCancel}>Cancel</Button>
<Button variant="danger" onClick={onConfirm}>Delete</Button>
</Dialog.Actions>
</Dialog>
);
}The generic Dialog stays small; each specific dialog is free to arrange its content however it needs. The React team’s long-standing advice is that composition covers the cases where other languages use class inheritance — you essentially never need components that “extend” other components.
Composition can avoid prop drilling
Suppose App has the current user and a deeply nested Avatar needs it:
// Passing user through every layer
<Layout user={user}> → <Sidebar user={user}> → <Avatar user={user} />If Layout and Sidebar don’t use user themselves, let them accept children instead, and build the deep part where the data is:
function App() {
const user = useCurrentUser();
return (
<Layout>
<Sidebar>
<Avatar user={user} />
</Sidebar>
</Layout>
);
}Layout and Sidebar just render {children} — they no longer need to know about users at all. Try this before reaching for context (covered in Context).
Inspecting children
Treat children as an opaque value: render it, but avoid poking at its structure. React has a Children utility (Children.map, Children.count) for the rare cases where you must, but it’s considered a legacy pattern — props like items or separate components (<Tabs.Tab>) are usually clearer and more robust.
Rule of thumb: when a component’s job is to frame content, give it
children. When it needs to place several pieces of content, give it named slot props.
A reusable Panel
Write a Panel component that takes title, an optional actions prop (JSX rendered in the header, aligned right) and children (the body). Render it with a Button in actions and a paragraph plus a list as children. Then write EmptyState({ icon, children }) and use it inside a Panel when a course list is empty.
export function Panel(/* props */) {
// your code here
}
export function EmptyState(/* props */) {
// your code here
}export function Panel({ title, actions, children }) {
return (
<section className="panel">
<header className="panel-header">
<h2>{title}</h2>
{actions && <div className="panel-actions">{actions}</div>}
</header>
<div className="panel-body">{children}</div>
</section>
);
}
export function EmptyState({ icon = '📭', children }) {
return (
<div className="empty">
<span aria-hidden="true">{icon}</span>
<p>{children}</p>
</div>
);
}
export function MyCourses({ courses }) {
return (
<Panel title="My courses" actions={<button>Browse catalogue</button>}>
{courses.length === 0 ? (
<EmptyState>You haven’t enrolled in any courses yet.</EmptyState>
) : (
<ul>
{courses.map((c) => <li key={c.id}>{c.title}</li>)}
</ul>
)}
</Panel>
);
}Summary
- Anything between a component’s opening and closing tags arrives as the
childrenprop. - Wrapper components (cards, layouts, modals) render
{children}without knowing what it is. - Pass JSX through other props to create multiple ‘slots’, such as
header,actionsorfooter. - Composition — combining small components — is React’s answer to inheritance.
- Passing JSX as children can also avoid threading props through many layers.