Full answer
A layout component, such as a Card, should not need to know what goes inside it. React solves this with the children prop. <Card title="Marks"><p>82 out of 100</p></Card> passes the paragraph as props.children. Card places it with {children}. Any content works: text, elements, arrays, even functions if you design for it.
A component must return a single value, but you often want two siblings, such as a heading and a paragraph. Wrapping them in a div adds an unneeded node, which can break CSS layouts like tables and grids. A fragment, written <>...</>, groups them with no output. Use the long form <Fragment key={id}> when a key is needed, for instance when each list item produces a term and a description.
Composition is how React reuses UI. Instead of making SpecialButton extends Button, you write a Button that accepts props and children, and wrap or configure it. You can also pass elements through named props, such as header and footer, which works as slots for more complex layouts. Composition keeps components small, predictable and easy to test, and it avoids prop drilling.
Some points to remember. children can be changed in structure using the Children utilities, but this is rare and fragile, so prefer explicit props. Passing JSX as children creates new elements on each render of the parent, which matters for memo. And a fragment with no key is fine in static positions.
The glossary component in the code uses all three ideas.
import { Fragment } from 'react';
function Card({ title, children, footer }) {
return (
<section>
<h2>{title}</h2>
<div>{children}</div>
<footer>{footer}</footer>
</section>
);
}
const terms = [
{ id: 'props', name: 'Props', meaning: 'Inputs passed from a parent.' },
{ id: 'state', name: 'State', meaning: 'Memory owned by a component.' },
];
export default function Glossary() {
return (
<Card title="React glossary" footer={<small>Updated weekly</small>}>
<dl>
{terms.map((term) => (
<Fragment key={term.id}>
<dt>{term.name}</dt>
<dd>{term.meaning}</dd>
</Fragment>
))}
</dl>
</Card>
);
}Card does not know what it wraps. A keyed Fragment lets each term produce two siblings (dt and dd) inside the dl element without an extra wrapper. Rendered result: <section><h2>React glossary</h2><div><dl><dt>Props</dt><dd>Inputs passed from a parent.</dd><dt>State</dt><dd>Memory owned by a component.</dd></dl></div><footer><small>Updated weekly</small></footer></section>
Key points
- children is whatever sits between a component's tags, rendered with {children}.
- Fragments group siblings without adding DOM nodes. Use the Fragment form when a key is needed.
- Composition (children, slot props) is the React way to reuse UI, instead of inheritance.
- Named props such as header and footer make multi-slot layouts clear.
- Prefer explicit props over manipulating children with the Children utilities.
Common mistakes
- Wrapping siblings in an extra div and breaking table or grid markup.
- Using the shorthand <> when a key is required inside a map.
- Reaching for inheritance to share UI between components.
- Forgetting to render {children}, so the content never appears.
Follow-ups
- How would you let a parent customise several areas of a child, such as header and footer?
- When would you use React.Children, and why is it discouraged?
- Why can passing JSX as children defeat memo?
Tips
- Explain that composition is the answer to "how do I reuse components". Many answers wander into HOCs.
- Show the keyed fragment case. It proves hands-on experience.
- Keep to one example that shows children and slots together.