Sub topic 1
Composition and the children prop
Prerequisites
This chapter assumes you can write function components and use props and JSX. You should also know how to pass functions as props and how ES modules and destructuring work. No hooks are needed here.
Why composition comes first
Composition means building a component by combining other components, instead of making one component configure everything through props. Every pattern later in this tutorial, including compound components, builds on it. React favors composition over inheritance. A component does not extend another one. It wraps or contains other components.
The children prop
Anything you put between a component's opening and closing tags arrives as the special children prop. The component decides where to render it.
function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
<div className="card-body">{children}</div>
</section>
);
}
export function Profile() {
return (
<Card title="Ada Lovelace">
<p>Mathematician and writer.</p>
<button onClick={() => alert('Followed')}>Follow</button>
</Card>
);
}Card knows nothing about what it contains. It can hold text, buttons, or other Card components. This is called containment, and it keeps Card from growing a new prop for every use case.
Specialization by wrapping
A specific component can be a configured version of a general one. It renders the general component and fills in some props.
function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
{children}
</section>
);
}
export function WarningCard({ children }) {
return (
<Card title="Warning">
<strong>Check this first:</strong> {children}
</Card>
);
}WarningCard passes its own children through to Card. Callers get a simpler API, and Card stays unchanged.
Common errors
- Forgetting to render
children. If the component never outputs{children}, the nested content silently disappears. - Overriding children with an explicit prop. If you write
<Card children="x">and also nest elements, the nested elements win. Prefer nesting. - Treating
childrenas an array. It can beundefined, a single element, a string, or an array. UseReact.Children.toArray(children)orReact.Children.mapif you need to iterate. Do not callchildren.mapdirectly. - Extra wrapper elements. Wrapping
{children}in adivyou do not need can break CSS layouts such as flexbox and grid. Use a fragment (<>...</>) when you need no element.
Where composition stops
children hands content to a component, but the content cannot see the component's internal state. A Card cannot tell its children whether it is expanded. The next chapters address this: lifting state up, render props, and context, which lead to compound components.
Exercise
A Panel component renders <div className="panel"><h3>{title}</h3></div>. A caller writes <Panel title="Info"><p>Hello</p></Panel>, but the paragraph never appears. What is the cause and the fix?
Show answer
Panel does not render its children prop, so the nested <p> is ignored. Destructure children in the props, function Panel({ title, children }), and render {children} inside the div, after the h3.