Children and composition

Topic 3 of 5 in JSX & components6 min readIntermediateFree course

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:

JSX
<Card title="Hooks">
  <p>Learn useState and useEffect.</p>
  <Button>Start</Button>
</Card>
JSX
function Card({ title, children }) {
  return (
    <section className="card">
      <h2>{title}</h2>
      {children}
    </section>
  );
}

Using Card with a title of Hooks, a paragraph and a Start button as children. The Card function renders a section with the title in an h2 followed by {children}; the paragraph and button appear in the children slot.

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:

JSX
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:

JSX
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:

JSX
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:

JSX
// ✗ 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:

JSX
// ✓ 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:

JSX
// 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:

JSX
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.

Exercise

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.

JavaScript
export function Panel(/* props */) {
  // your code here
}

export function EmptyState(/* props */) {
  // your code here
}

Summary

  • Anything between a component’s opening and closing tags arrives as the children prop.
  • Wrapper components (cards, layouts, modals) render {children} without knowing what it is.
  • Pass JSX through other props to create multiple ‘slots’, such as header, actions or footer.
  • Composition — combining small components — is React’s answer to inheritance.
  • Passing JSX as children can also avoid threading props through many layers.