Composition with children

Whatever you put between a component's tags arrives as the children prop. Build a Card that can wrap any content.

React · beginner · 5 min · 5 steps · free

What you'll learn

Children.jsx

function Card({ title, children }) {
  return (
    <section className="card">
      <h3>{title}</h3>
      {children}
    </section>
  );
}

export default function App() {
  return (
    <Card title="Shopping list">
      <ul>
        <li>Milk</li>
        <li>Bread</li>
      </ul>
    </Card>
  );
}

Step by step

  1. React renders <App> for the first time: it calls the App function.
  2. App puts a list between <Card> and </Card>. That content is passed to Card as a prop named children.
  3. React calls Card with title and children.
  4. {children} places the list inside the section, under the heading. The State panel shows children as the <ul> JSX.
  5. The card wraps the list: a reusable box for any content.

Common mistakes

Best practices

Key takeaways

Check yourself

<Card><p>Hi</p></Card>: what is children?

Answer: <p>Hi</p>. The content between the tags.

If Card never renders {children}, the <p> is?

Answer: Not shown. React only shows what the component returns.

·

More in Components and JSX