React Is

19.2.5 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates the core utility functions of `react-is` by checking various React component types and element instances. This example covers `isValidElementType`, `isElement`, `isFragment`, `isMemo`, and `isForwardRef` to differentiate between different forms of React nodes and types.

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();

view raw JSON →