Skip to content

Children

The Course
•
15 min study

Props work well when you know what kind of data a component needs: a title, a number, a flag. Some components are frames for any content. A card, a panel or a dialog shouldn't care what goes inside it.

For that, React has a special prop called children. Whatever you put between a component's opening and closing tags arrives as children:

JSX
<Card title="Props">
  <p>Props pass data from a parent to a child.</p>
</Card>

Inside Card, {children} marks where that content goes.

A Card that wraps anything React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Card from "./Card.jsx"; export default function App() { return ( <main> <h1>Study notes</h1> <Card title="Props"> <p>Props pass data from a parent to a child.</p> </Card> <Card title="Today's plan"> <ol> <li>Read the lesson</li> <li>Change the lab</li> <li>Do the exercise</li> </ol> </Card> </main> ); } export default function Card({ title, children }) { return ( <section className="card"> <h2>{title}</h2> {children} </section> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Both cards use the same Card component. One wraps a paragraph and the other wraps a numbered list. Card supplies the frame and the heading, and the parent decides what goes inside:

JSX
export default function Card({ title, children }) {
  return (
    <section className="card">
      <h2>{title}</h2>
      {children}
    </section>
  );
}

Predict

Add a third card to App.jsx with no children:

JSX
<Card title="Empty" />

What does it show? Run it to check.

Check your prediction

A card with the heading Empty and nothing under it. Without content between the tags, children is undefined, and React shows nothing for it.

Composition

Building bigger pieces from smaller ones like this is called composition. It's how React apps grow: App is made of cards, a card is made of a heading and its children, and each piece stays small enough to read in one go.

Use children when the content varies a lot from one use to the next. Use ordinary props when the component needs specific values it formats itself, like DayCard's minutes.

Your turn

Exercise

A Tip box

Write a Tip component that shows the word Tip: in bold, followed by its children, inside a <p>. Use it twice in App:

  • Tip: Run after every small change.
  • Tip: Read the console before the code.
Your working space React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Card from "./Card.jsx"; export default function App() { return ( <main> <h1>Study notes</h1> <Card title="Props"> <p>Props pass data from a parent to a child.</p> </Card> <Card title="Today's plan"> <ol> <li>Read the lesson</li> <li>Change the lab</li> <li>Do the exercise</li> </ol> </Card> </main> ); } export default function Card({ title, children }) { return ( <section className="card"> <h2>{title}</h2> {children} </section> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

Tip receives { children } and returns one <p>. Bold text is <strong>...</strong>.

Hint 2: a first step

<Tip>Run after every small change.</Tip>. Text between the tags is children too, not only elements.

Show one solution
JSX
function Tip({ children }) {
  return (
    <p>
      <strong>Tip:</strong> {children}
    </p>
  );
}

And in App:

JSX
<Tip>Run after every small change.</Tip>
<Tip>Read the console before the code.</Tip>

You can define Tip in App.jsx above App, or in its own file with a default export.

Was this article helpful?

Your response is saved on this device.