Skip to content
tezvyn:

How does children work, and why is it fundamental to composition?

Source: react.devEasyHow cards are made

How does children work, and why is it fundamental to composition?

Tests React composition knowledge. Good answer: children is a prop holding nested JSX; a Card wrapper renders {children} to let parents inject markup; contrast with prop-driven text. Red flag: calling children magic syntax instead of a standard prop.

What's really being asked

This question probes whether you understand React's core composition model and can distinguish it from configuration via props. The interviewer wants to see that you treat children as a first-class prop, not magic syntax, and that you design wrapper components which delegate content responsibility upward rather than hardcoding it internally.

The full answer

A good answer covers four things in order: first, state that children is simply a prop containing whatever JSX is placed between a component's opening and closing tags; second, describe a wrapper component like Card that returns a styled container div and renders the children prop inside it, letting the parent decide what goes in; third, contrast this with the less flexible approach of passing content through a custom prop such as body or text, explaining that composition keeps presentational wrappers decoupled from their contents; and fourth, note that because children is a prop, you can pass it around, conditionally render it, or manipulate it programmatically just like any other JavaScript value.

The mistakes people make

Common wrong answers include treating children as special syntax rather than a standard prop on the props object; building a Card that accepts a content prop instead of children, which limits consumers to strings or forces extra prop drilling for nested markup; claiming that children can only be a single element, when React actually passes arrays, fragments, or strings; and failing to explain why composition is preferable, such as avoiding deep prop drilling or keeping layout and content concerns separate.

What usually comes next

Likely follow-ups include how you would validate or restrict the types of children a component receives; when you would choose a render prop over children for customization; how the React Children API helps if you need to inspect or augment each child; and what happens to performance if you inline JSX as children versus referencing a stable variable.

A concrete example

Imagine a Card component used like this: Card opening tag, then an h2 heading with Order Summary, then a paragraph with item details, then a button, then Card closing tag. The Card component itself only controls border, padding, and shadow, rendering the children prop inside a div. It never imports heading or button components. This means the same Card can wrap a login form, a product image, or an error message without any internal changes, proving that composition scales where prop-based configuration breaks down.

Interview question

When designing a reusable Card component that should wrap arbitrary content like headings, paragraphs, or buttons, why is using children preferable to using a dedicated body prop?

  • a.Because the Card can remain a presentational wrapper that does not need to know what markup it containsCorrect
  • b.Because children is handled by special React syntax that automatically optimizes re-renders for nested JSX
  • c.Because custom props like body cannot accept JSX values and are restricted to plain strings
  • d.Because children can only contain a single element, making the Card's output more predictable and easier to style
Why?

Using children lets the Card stay a layout wrapper while the parent decides the content, avoiding tight coupling. The tempting idea that body props only accept strings is wrong—custom props can technically receive JSX, but using them for composition forces the wrapper to know about specific content structures.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles