Course contentsShow
← TypeScript
Lesson 1519 of 1,79635. TypeScript with Framework Cross-ReferencesFree lesson

Children Props and ReactNode

Type the children prop correctly using ReactNode, ReactElement, and JSX.Element distinctions.

Children Props and ReactNode

You'll learn: How to type the children prop correctly using ReactNode, ReactElement, and JSX.Element, and when to use each.

What Is the Children Prop?

In React components, children is a special prop that represents content passed between opening and closing tags. In TypeScript, you need to type this prop correctly to ensure type safety while maintaining React's flexibility.

The problem? React's children can be many different things: text, numbers, arrays of components, null, functions, or even nothing at all.

The Three Key Types

ReactNode: The Flexible Parent Type

ReactNode is the broadest, most permissive type. It accepts almost anything React can render:

import { ReactNode } from 'react';

interface CardProps {
  children: ReactNode;
}

function Card({ children }: CardProps) {
  return <div className="card">{children}</div>;
}

// All valid:
<Card>Hello</Card>
<Card>{123}</Card>
<Card><div>Nested</div></Card>
<Card>{null}</Card>
<Card>{['a', 'b', 'c']}</Card>

Use ReactNode when: You want maximum flexibility and don't care about the exact structure of children.

ReactElement: Single React Elements Only

ReactElement is stricter—it only accepts actual React elements created with JSX, not primitives or arrays:

import { ReactElement } from 'react';

interface WrapperProps {
  children: ReactElement;
}

function Wrapper({ children }: WrapperProps) {
  return <section>{children}</section>;
}

// Valid:
<Wrapper><div>Content</div></Wrapper>

// Type errors:
<Wrapper>Plain text</Wrapper>          // Error: string not assignable
<Wrapper>{123}</Wrapper>                // Error: number not assignable
<Wrapper><div>A</div><div>B</div></Wrapper> // Error: multiple children

Use ReactElement when: You need exactly one React component/element as a child.

JSX.Element: Similar to ReactElement

JSX.Element is the return type of JSX expressions. It's functionally similar to ReactElement but slightly less flexible with generics:

interface BoxProps {
  children: JSX.Element;
}

Most developers prefer ReactElement for props because it allows for more precise generic constraints, though in practice they're interchangeable for basic use cases.

Choosing the Right Type

Think of these as a hierarchy of strictness:

  • ReactNode = "I'll take anything React can render" (strings, numbers, elements, arrays, null)
  • ReactElement = "I need actual JSX elements only" (no primitives)
  • JSX.Element = "Similar to ReactElement" (slightly less common for props)

Key Takeaway: Use ReactNode for flexible children props that accept any renderable content, ReactElement when you need exactly one React element, and avoid JSX.Element in favor of ReactElement for better generic support.