Part 02 / Topic 3 of 7
children: the simplest form of composition
Composition means a UI is a tree of components calling other components, the same way a program is a tree of functions calling other functions - Part 1's WalletSummary already reused a small Panel component twice this way. Whatever JSX sits between a component's opening and closing tag arrives as a prop named children, automatically, with no extra wiring:
// examples/02_children_and_slots.jsx
export function PageSection({ header, children, footer }) {
return (
<section className="page-section">
{header && <header className="page-section-header">{header}</header>}
<div className="page-section-body">{children}</div>
{footer && <footer className="page-section-footer">{footer}</footer>}
</section>
);
}
Rendering <PageSection header={<h2>Notes</h2>}><p>Leave the parcel with the watchman</p></PageSection> with no footer prop at all, and reading the result with Testing Library, really shows (React 19.3.1, examples/02_children_and_slots.test.jsx):
Notes
Leave the parcel with the watchman
and there is no <footer> element in the output at all - footer && ... is plain JavaScript short-circuiting, the exact same && trick Part 1's prerequisites assume you already know, just used inside JSX. Leaving a slot prop out entirely is different from passing it an empty string or 0 - section 5 of Part 1 already warned about "0 renders as text," and Part 4 covers that trap properly; header &&/footer && here only works safely because header and footer are either real JSX or undefined, never a falsy number.
Done here? The practice set for Part 2 — including the solutions — is on the part page.