React Is
react-is is a low-level utility package provided by the React team for performing "brand checking" on React elements and types. This library exports a set of predicate functions (e.g., `isElement`, `isValidElementType`, `isFragment`, `isMemo`) that allow developers, particularly library authors, to programmatically determine the specific type or "brand" of a React node without relying on potentially unstable internal properties. The current stable version is 19.2.5, aligning with the major version of React itself, indicating a close release cadence tied to the main React project. Its primary differentiation is providing a stable, official API for introspecting React elements, which is crucial for tools, renderers, and component libraries that need to handle various React node types robustly, unlike direct checks on `$$typeof` which are considered internal and unstable. It does not provide UI components but rather helper functions for type introspection.
Common errors
-
TypeError: (0 , react_is__WEBPACK_IMPORTED_MODULE_0__.isValidElementType) is not a function
cause This error typically occurs in modern module environments (like Webpack or Vite) when attempting to use a CommonJS `require` statement for named ES module exports, or when a bundler misinterprets the module format. `react-is` uses named ES module exports.fixEnsure you are using ES module `import { isValidElementType } from 'react-is'` syntax. If using TypeScript, check your `tsconfig.json` for correct `moduleResolution` settings (e.g., `bundler` or `node16`). -
Uncaught TypeError: Super expression must either be null or a function
cause This error often indicates that React tried to render a component whose type was not a function or class, or was `null` or `undefined`. While not directly from `react-is`, failing to use `react-is` to validate component types can lead to such runtime errors if invalid types are passed to rendering functions.fixBefore rendering, use `isValidElementType` to ensure that a value intended to be a React component type is actually valid. For example, `if (isValidElementType(MyComponent)) { return <MyComponent />; }`.
Warnings
- breaking The `react-is` package closely tracks the major version of `react`. While `react-is` itself rarely introduces breaking API changes, using an incompatible major version (e.g., `react-is@19` with `react@18`) can lead to subtle bugs or incorrect type checks due to internal changes in React's element structure. Always ensure `react-is` and `react` major versions are aligned.
- gotcha `react-is` is primarily designed for library authors and advanced tooling that needs to deeply introspect React elements and types. For most application-level logic, directly using `React.isValidElement()` (to check if something is an element instance) or `typeof` checks for basic component types might be sufficient and simpler. Over-reliance on `react-is` for simple cases can lead to unnecessary complexity.
- gotcha Confusing `isValidElementType` with `isElement` (or `React.isValidElement`). `isValidElementType(value)` checks if `value` is a *type* that React can render (e.g., a function component, class component, string literal like 'div', or a special type like `React.Fragment`). `isElement(value)` (or `React.isValidElement(value)`) checks if `value` is an *instance* of a React element (the result of calling `React.createElement` or JSX transformation).
Install
-
npm install react-is -
yarn add react-is -
pnpm add react-is
Imports
- isValidElementType
const { isValidElementType } = require('react-is')import { isValidElementType } from 'react-is' - isElement
import isElement from 'react-is'
import { isElement } from 'react-is' - isFragment
import { isFragment } from 'react-is' - Memo
import { isMemo } from 'react-is'
Quickstart
import React from 'react';
import { isValidElementType, isElement, isFragment, isMemo, isForwardRef } from 'react-is';
const MyComponent = ({ children }) => <div>{children}</div>;
const MemoizedComponent = React.memo(MyComponent);
const ForwardRefComponent = React.forwardRef((props, ref) => <div ref={ref} />);
function checkReactTypes() {
// 1. Check if a value is a valid React component type (e.g., string, class, function, Fragment)
console.log('Is `MyComponent` a valid element type?', isValidElementType(MyComponent));
console.log('Is `"div"` a valid element type?', isValidElementType('div'));
console.log('Is `null` a valid element type?', isValidElementType(null));
// 2. Check if a value is a React element instance (the result of React.createElement)
const elementInstance = <MyComponent />;
const divElement = <div>Hello</div>;
console.log('Is `elementInstance` a React element?', isElement(elementInstance));
console.log('Is `divElement` a React element?', isElement(divElement));
console.log('Is `MyComponent` (the component itself) a React element?', isElement(MyComponent));
// 3. Check for specific React special types
console.log('Is `React.Fragment` a fragment type?', isFragment(React.Fragment));
console.log('Is `MemoizedComponent` a memo type?', isMemo(MemoizedComponent));
console.log('Is `ForwardRefComponent` a forwardRef type?', isForwardRef(ForwardRefComponent));
}
checkReactTypes();