Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Top-Level React APIs

React isValidElement() function

Reference for React.isValidElement: test whether a value is a React element, what it returns for strings, components and plain objects, and common uses.

isValidElement(value) returns true if value is a React element: the object produced by JSX or createElement. Anything else gives false: strings, numbers, null, arrays, plain objects and component functions.

You use it when a component accepts flexible content, such as an icon prop that may be a string or an element, and needs to treat the two differently, or before calling cloneElement, which only accepts elements.

JSX
import { isValidElement } from 'react';

isValidElement(<b>hi</b>); // true
isValidElement('hi'); // false

Try it

What counts as an element?
An icon prop that takes text or an element

Syntax

TypeScript
function isValidElement(value: unknown): value is ReactElement;
Name Description
value Any value to test.

Returns: true for elements, otherwise false. It never throws. In TypeScript it narrows the type to ReactElement.

Usage notes

  • A component is not an element. Card is a function (false); <Card /> is an element (true). Passing the wrong one as a prop is a classic mistake that produces "Functions are not valid as a React child".
  • A rendered result is an element too. Elements returned by createElement, JSX and cloneElement all pass. Text, numbers, null, booleans and arrays do not, even though they are valid children (a ReactNode).
  • Not every renderable value is an element. Use isValidElement for "is this a single element?", not for "can React render this?".
  • Plain objects that look like elements fail. The check relies on an internal marker, so hand-built { type, props } objects return false.
  • Fragments (<>...</>) are elements. The value returned by createPortal is not, so it gives false.
  • Pair it with Children. Children.toArray(children).filter(isValidElement) leaves only element children, dropping text.
  • Cost: it is a constant-time property check, safe to call during render.