react-is
This package allows you to test arbitrary values and see if they're a particular React element type.
Installation
yarn add react-is
npm install react-is
Usage
Determining if a Component is Valid
import * as ReactIs from "react-is";
class ClassComponent extends React.Component {
render() {
return React.createElement("div");
}
}
const StatelessComponent = () => React.createElement("div");
const ForwardRefComponent = React.forwardRef((props, ref) =>
React.createElement(Component, { forwardedRef: ref, ...props })
);
const Context = React.createContext(false);
ReactIs.isValidElementType("div");
ReactIs.isValidElementType(ClassComponent);
ReactIs.isValidElementType(StatelessComponent);
ReactIs.isValidElementType(ForwardRefComponent);
ReactIs.isValidElementType(Context.Provider);
ReactIs.isValidElementType(Context.Consumer);
ReactIs.isValidElementType(React.createFactory("div"));
Determining an Element's Type
ConcurrentMode
import React from "react";
import * as ReactIs from 'react-is';
ReactIs.isConcurrentMode(<React.unstable_ConcurrentMode />);
ReactIs.typeOf(<React.unstable_ConcurrentMode />) === ReactIs.ConcurrentMode;
Context
import React from "react";
import * as ReactIs from 'react-is';
const ThemeContext = React.createContext("blue");
ReactIs.isContextConsumer(<ThemeContext.Consumer />);
ReactIs.isContextProvider(<ThemeContext.Provider />);
ReactIs.typeOf(<ThemeContext.Provider />) === ReactIs.ContextProvider;
ReactIs.typeOf(<ThemeContext.Consumer />) === ReactIs.ContextConsumer;
Element
import React from "react";
import * as ReactIs from 'react-is';
ReactIs.isElement(<div />);
ReactIs.typeOf(<div />) === ReactIs.Element;
Fragment
import React from "react";
import * as ReactIs from 'react-is';
ReactIs.isFragment(<></>);
ReactIs.typeOf(<></>) === ReactIs.Fragment;
Portal
import React from "react";
import ReactDOM from "react-dom";
import * as ReactIs from 'react-is';
const div = document.createElement("div");
const portal = ReactDOM.createPortal(<div />, div);
ReactIs.isPortal(portal);
ReactIs.typeOf(portal) === ReactIs.Portal;
StrictMode
import React from "react";
import * as ReactIs from 'react-is';
ReactIs.isStrictMode(<React.StrictMode />);
ReactIs.typeOf(<React.StrictMode />) === ReactIs.StrictMode;