1 | # `react-is`
|
2 |
|
3 | This package allows you to test arbitrary values and see if they're a particular React element type.
|
4 |
|
5 | ## Installation
|
6 |
|
7 | ```sh
|
8 | # Yarn
|
9 | yarn add react-is
|
10 |
|
11 | # NPM
|
12 | npm install react-is
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ### Determining if a Component is Valid
|
18 |
|
19 | ```js
|
20 | import React from "react";
|
21 | import * as ReactIs from "react-is";
|
22 |
|
23 | class ClassComponent extends React.Component {
|
24 | render() {
|
25 | return React.createElement("div");
|
26 | }
|
27 | }
|
28 |
|
29 | const StatelessComponent = () => React.createElement("div");
|
30 |
|
31 | const ForwardRefComponent = React.forwardRef((props, ref) =>
|
32 | React.createElement(Component, { forwardedRef: ref, ...props })
|
33 | );
|
34 |
|
35 | const Context = React.createContext(false);
|
36 |
|
37 | ReactIs.isValidElementType("div"); // true
|
38 | ReactIs.isValidElementType(ClassComponent); // true
|
39 | ReactIs.isValidElementType(StatelessComponent); // true
|
40 | ReactIs.isValidElementType(ForwardRefComponent); // true
|
41 | ReactIs.isValidElementType(Context.Provider); // true
|
42 | ReactIs.isValidElementType(Context.Consumer); // true
|
43 | ReactIs.isValidElementType(React.createFactory("div")); // true
|
44 | ```
|
45 |
|
46 | ### Determining an Element's Type
|
47 |
|
48 | #### ConcurrentMode
|
49 |
|
50 | ```js
|
51 | import React from "react";
|
52 | import * as ReactIs from 'react-is';
|
53 |
|
54 | ReactIs.isConcurrentMode(<React.unstable_ConcurrentMode />); // true
|
55 | ReactIs.typeOf(<React.unstable_ConcurrentMode />) === ReactIs.ConcurrentMode; // true
|
56 | ```
|
57 |
|
58 | #### Context
|
59 |
|
60 | ```js
|
61 | import React from "react";
|
62 | import * as ReactIs from 'react-is';
|
63 |
|
64 | const ThemeContext = React.createContext("blue");
|
65 |
|
66 | ReactIs.isContextConsumer(<ThemeContext.Consumer />); // true
|
67 | ReactIs.isContextProvider(<ThemeContext.Provider />); // true
|
68 | ReactIs.typeOf(<ThemeContext.Provider />) === ReactIs.ContextProvider; // true
|
69 | ReactIs.typeOf(<ThemeContext.Consumer />) === ReactIs.ContextConsumer; // true
|
70 | ```
|
71 |
|
72 | #### Element
|
73 |
|
74 | ```js
|
75 | import React from "react";
|
76 | import * as ReactIs from 'react-is';
|
77 |
|
78 | ReactIs.isElement(<div />); // true
|
79 | ReactIs.typeOf(<div />) === ReactIs.Element; // true
|
80 | ```
|
81 |
|
82 | #### Fragment
|
83 |
|
84 | ```js
|
85 | import React from "react";
|
86 | import * as ReactIs from 'react-is';
|
87 |
|
88 | ReactIs.isFragment(<></>); // true
|
89 | ReactIs.typeOf(<></>) === ReactIs.Fragment; // true
|
90 | ```
|
91 |
|
92 | #### Portal
|
93 |
|
94 | ```js
|
95 | import React from "react";
|
96 | import ReactDOM from "react-dom";
|
97 | import * as ReactIs from 'react-is';
|
98 |
|
99 | const div = document.createElement("div");
|
100 | const portal = ReactDOM.createPortal(<div />, div);
|
101 |
|
102 | ReactIs.isPortal(portal); // true
|
103 | ReactIs.typeOf(portal) === ReactIs.Portal; // true
|
104 | ```
|
105 |
|
106 | #### StrictMode
|
107 |
|
108 | ```js
|
109 | import React from "react";
|
110 | import * as ReactIs from 'react-is';
|
111 |
|
112 | ReactIs.isStrictMode(<React.StrictMode />); // true
|
113 | ReactIs.typeOf(<React.StrictMode />) === ReactIs.StrictMode; // true
|
114 | ```
|