Course contentsShow
← React
Lesson 2342 of 2,39651. TypeScript with React BasicsFree lesson

When to Use ReactNode vs ReactElement

Decide between ReactNode for flexible APIs and ReactElement when you need guaranteed JSX for manipulation or cloning.

When to Use ReactNode vs ReactElement

What you'll learn: How to choose the right type between ReactNode and ReactElement based on whether you need flexibility or guaranteed JSX structure.

The Core Decision

You've seen both ReactNode and ReactElement as types for children or props that accept renderable content. The choice between them comes down to what you plan to do with that content.

Use ReactNode when you're building a flexible API that simply renders whatever it receives. This includes components like layout wrappers, containers, or anything that just passes children through to the DOM.

Use ReactElement when you need to manipulate, inspect, or clone the JSX element. If your component needs to inject props, read element types, or ensure it's working with actual JSX (not strings or numbers), you need the stricter type.

The Practical Difference

// Flexible API: just renders whatever
interface CardProps {
  children: ReactNode; // strings, numbers, JSX, null, arrays—all welcome
}

// Needs to manipulate JSX: must be an element
interface CloneWithPropsProps {
  element: ReactElement; // ONLY actual JSX elements
  extraProp: string;
}

function CloneWithProps({ element, extraProp }: CloneWithPropsProps) {
  return React.cloneElement(element, { extraProp }); // This requires ReactElement
}

If you tried passing ReactNode to React.cloneElement(), TypeScript would error because cloneElement specifically requires a ReactElement—it can't clone a string or number.

Think of It This Way

ReactNode is like accepting "anything displayable on screen." ReactElement is like requiring "a specific object I can open up and modify."

Key Takeaway: Choose ReactNode for flexible rendering APIs and ReactElement when you need guaranteed JSX structure for manipulation or cloning operations.