React Children: How to Use a React Component with Children

A React component with children is a wrapper: whatever you put between its opening and closing tags arrives in the children prop. Render {children} where that content should appear:

function Panel({ children }: { children: ReactNode }) {
  return <section className="panel">{children}</section>;
}

In this tutorial, I’ll use a small store dashboard to show how React children work, how to type them in TypeScript, and how React.Children counts and maps them.

How to Use Children in a React Component

Here’s the sample: a dashboard for Ridgeline Coffee Co., a made-up Denver shop, where one Panel frames two blocks of content (app set up with Vite and React):

import type { ReactNode } from "react";
type PanelProps = { title: string; children: ReactNode };
function Panel({ title, children }: PanelProps) {
  return (
    <section className="panel">
      <h2>{title}</h2>
      <div className="body">{children}</div>
    </section>
  );
}

export default function App() {
  return (
    <main>
      <Panel title="Today's Orders">
        <p>42 orders, $1,286.50 in sales</p>
      </Panel>
      <Panel title="Low Stock">
        <ul>
          <li>Oat milk: 3 cartons</li>
          <li>Colombia beans: 2 bags</li>
        </ul>
      </Panel>
    </main>
  );
}
Two React Panel components on a coffee shop dashboard, one showing today's orders and one a low stock list passed as children
Same Panel, two different children: a sales line and a stock list.

Each title is a normal prop. The <p> and <ul> aren’t attributes; they’re collected into children. You can see it in each .body div:

<div class="body"><p>42 orders, $1,286.50 in sales</p></div>
<div class="body"><ul><li>Oat milk: 3 cartons</li><li>Colombia beans: 2 bags</li></ul></div>

So a children component in React only decides where the content goes; the parent decides what it is. It’s the containment pattern from my React composition patterns post, and a Next.js layout.tsx gets each page the same way.

VS Code with the React App.tsx Panel component and the tsc and Vite dev server output in the terminal
App.tsx in VS Code: tsc passes and the Vite dev server starts.

How Do You Type children in React with TypeScript?

Type React component children with ReactNode. It accepts anything renderable: JSX, strings, numbers, arrays, null. My React component types tutorial covers the rest of the props typing.

PropsWithChildren adds an optional children for you and compiles the same:

import type { PropsWithChildren } from "react";

type PanelProps = PropsWithChildren<{ title: string }>;

Leave children out of the props type and TypeScript (and VS Code) rejects every <Panel> that wraps content:

type PanelProps = { title: string };

function Panel({ title }: PanelProps) {
  return (
    <section className="panel">
      <h2>{title}</h2>
error TS2322: Type '{ title: string; children: Element; }' is not assignable to type 'IntrinsicAttributes & PanelProps'.
VS Code showing a red squiggle on the Panel tag and a TS2322 children error from tsc in the terminal
The Panel tag gets a red squiggle and tsc a TS2322 error.

Add children: ReactNode back and the error goes away. React 19’s FC type doesn’t add children either.

React.Children: Count and Map a Component’s Children

Sometimes you need each child, say to number steps. Same coffee shop, now a pour-over recipe card. Children.count and Children.map handle that:

import { Children } from "react";
import type { ReactNode } from "react";

function Steps({ children }: { children: ReactNode }) {
  const total = Children.count(children);
  return (
    <ol>
      {Children.map(children, (child, i) => (
        <li>Step {i + 1} of {total}: {child}</li>
      ))}
    </ol>
  );
}

export default function App() {
  return (
    <section className="panel">
      <h2>Pour-Over Recipe</h2>
      <Steps>
        <span>Grind 22 g of beans</span>
        <span>Bloom with 50 g of water for 30 s</span>
        <span>Pour to 350 g by 2:30</span>
      </Steps>
    </section>
  );
}
React Steps component numbering three span children as Step 1 of 3 to Step 3 of 3 on a pour-over recipe card
Children.map wrapped each span in an li with its number.

You get:

Step 1 of 3: Grind 22 g of beans
Step 2 of 3: Bloom with 50 g of water for 30 s
Step 3 of 3: Pour to 350 g by 2:30

Children.map also gives each child a key, so there’s no missing-key warning (more on the React key prop).

Important: a hidden child still counts. Switch the bloom step off with false &&:

<Steps>
  <span>Grind 22 g of beans</span>
  {false && <span>Bloom with 50 g of water for 30 s</span>}
  <span>Pour to 350 g by 2:30</span>
</Steps>
Step 1 of 3: Grind 22 g of beans
Step 2 of 3: 
Step 3 of 3: Pour to 350 g by 2:30

Children.count still said 3, and Children.map rendered an empty Step 2. That fragility is why React lists Children under its legacy APIs; an array prop is usually sturdier.

Why Passed Children Don’t Show Up in React

The usual cause in React is a component that accepts children but never renders it. Ridgeline’s Panel without the {children} line:

function Panel({ title, children }: PanelProps) {
  return (
    <section className="panel">
      <h2>{title}</h2>
    </section>
  );
}

The browser got only the headings:

<section class="panel"><h2>Today's Orders</h2></section>
<section class="panel"><h2>Low Stock</h2></section>

You get no error. If the whole component is missing, work through why a React component is not rendering.

Related React tutorials on parent and child components:

React Children Questions, Answered

What is props.children in React?

The content between a component’s opening and closing tags, passed in as a prop named children.

Can a React component have more than one children slot?

One children, but any prop can take JSX, like header={<h2>Sales</h2>}.

What type should children be in TypeScript?

ReactNode, or ReactElement to accept JSX elements only.

Is children.length safe to use?

No. A single child isn’t an array, so use Children.count or Children.toArray.

Can children be a function?

Yes. That’s the render prop pattern: the component calls children(data).

Where is React.Children documented?

The React Children reference lists every method.

Leave a Comment