Skip to content
Core ConceptsChapter 7 / 37Intermediate4 min read

Children & Composition

Learn what the React children prop and composition are, how to put components inside each other, and how to reduce prop drilling.

On this page

1. What are children and composition?

Composition means building a big UI by combining small components. Instead of inheritance (extending a class), React composes components by putting them inside each other.

Just as you can put anything inside a div in HTML, you can put anything inside your own Card or Modal.

2. Why does it matter?

Many components only exist to make a "box": a border, padding, a title. What goes inside is not known in advance.

Without childrenWith children
A new prop for every variationThe parent writes the inner content itself
Only text can be passed into the cardAny JSX inside the card
Data must be sent down many levelsPlace the component directly in the right spot
Rigid, hard to reuseFlexible wrapper components

3. Where is it used?

Use caseExample
LayoutA PageLayout with a header, a sidebar and children as the content
Card / PanelA wrapper with a border and padding
Modal / DialogAny form or message inside an overlay
ProvidersA context provider that wraps the whole app
SlotsPassing JSX in separate props like header and footer

4. How does it work?

When you write something between the tags, React automatically puts it in a prop named children.

Children flow
The parent writes JSX between the tags
React puts that JSX in props.children
The wrapper component renders children anywhere it likes
The final UI has the wrapper + the content inside

The wrapper does not know what is inside, and it should not need to. That is the power of composition.

5. Code example

Basic children

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

export default function App() {
  return (
    <Card title="Profile">
      <p>Name: Ayesha</p>
      <button>Edit</button>
    </Card>
  );
}

Multiple slots

jsx
function PageLayout({ sidebar, children }) {
  return (
    <div className="layout">
      <aside>{sidebar}</aside>
      <main>{children}</main>
    </div>
  );
}

export default function App() {
  return (
    <PageLayout sidebar={<nav>Home | Settings</nav>}>
      <h1>Dashboard</h1>
    </PageLayout>
  );
}

How does the code work?

ItemMeaning
{ title, children }children is a prop just like the other props
{children}The inner JSX renders exactly where you write it
sidebar={<nav>...</nav>}You can also pass JSX in a normal prop (a slot)
<Card>...</Card>Opening and closing tags, the content between them is children

Reducing prop drilling

If App has user and it needs to reach Avatar through Layout, Layout does not need to receive user. App can build <Avatar user={user} /> itself and give it to Layout as children. The middle component never touches the data.

6. Common mistakes

Mistake: Forgetting to render children in the wrapper component

Why? If children is not written anywhere in the return, the inner content will not appear on the screen and you will not get any error either.

Better: Decide the spot in the wrapper's JSX where children should render.

Mistake: Creating a new prop for every small thing

Why? A long list of props like title, subtitle, icon and footerText makes the component rigid.

Better: Turn the part that should be flexible into children or a JSX slot prop.

Mistake: Sending data down five levels through props

Why? The middle components do not need that data, yet they must pass it along.

Better: Try composition first, build the component higher up and pass it in as children. Use context if needed.

Interview tip

If you get the "composition vs inheritance" question, say: in React, code reuse happens through composition, and components are not extended.

7. What to learn before and after

StageChapters
BeforeProps, Components
AfteruseContext, Portals
RelatedLifting State Up

Final quick revision

PointDetail
childrenThe JSX written between the tags
CompositionA big UI built from small components
SlotsPassing JSX in named props
BenefitFlexible wrappers, less prop drilling