liveBook · Manning

Child

As explained in React in Depth

The special React prop containing the values nested between a component's opening and closing JSX tags.

The special React prop child values nested between a component’s opening and closing JSX tags. Typing it narrowly as string fails when JSX combines text, expressions, booleans, numbers, elements, arrays, or no value. section 5.3.2

Choose the type according to the component’s intended content:

  • Use string only for components that deliberately accept plain text. section 5.3.2
  • Avoid manually expanding unions for every JSX case; ReactNode covers the relevant child values and additional edge cases. section 5.3.2
  • Use PropsWithChildren when combining ordinary component props with the standard children prop. section 5.3.2

Typed heading with optional children

import { type PropsWithChildren } from 'react'; interface HeadingProps { isLarge?: boolean; } function Heading({ isLarge = false, children }: PropsWithChildren<HeadingProps>) { return <h1 style={{ backgroundColor: "hotpink", fontSize: isLarge ? '48px' : '36px' }}>{children}</h1>; } <Heading isLarge>This is important!</Heading> section 5.3.2

import { type ReactNode } from 'react';
interface HeadingProps {
  children?: ReactNode;
}
function Heading({ children }: HeadingProps) {
  ...
}

Inspect the concise ReactNode declaration for a component accepting optional children. section 5.3.2

How React in Depth covers it

How deep it goes
Works from string typing through failing dynamic and formatted cases to ReactNode and PropsWithChildren, without exploring further edge cases.
Examples
Uses a Heading component with text, dynamic expressions, booleans, numbers, strong formatting, omitted children, and an isLarge prop.
What it assumes
Assumes familiarity with React components, JSX, props, and basic TypeScript interfaces.
What you can do after
Apply it via ReactNode or PropsWithChildren to type reusable components without manually enumerating every supported child form.
How it connects
Connects child with JSX, TypeScript, ReactNode, PropsWithChildren, component props, and reusable React components.

Also explained in

Compare how 3 books explain child →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.