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.
import { isValidElement } from 'react'; isValidElement(<b>hi</b>); // true isValidElement('hi'); // false
Try it
Syntax
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.
Cardis 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 andcloneElementall pass. Text, numbers,null, booleans and arrays do not, even though they are valid children (aReactNode). - Not every renderable value is an element. Use
isValidElementfor "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 returnfalse. - Fragments (
<>...</>) are elements. The value returned bycreatePortalis not, so it givesfalse. - 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.