1 | # Is React
|
2 |
|
3 | <p align="center">
|
4 | <a href="https://www.npmjs.com/package/is-react">
|
5 | <img alt="is-react?" src="https://github.com/treyhuffine/is-react/raw/master/is-react.png" width="300">
|
6 | </a>
|
7 | </p>
|
8 |
|
9 | <p align="center">
|
10 | Determine if a variable or expression is a valid element or component in <a href="https://facebook.github.io/react">React</a>
|
11 | </p>
|
12 |
|
13 | <p align="center">
|
14 | <a href="https://www.npmjs.com/package/is-react"><img src="https://img.shields.io/npm/v/is-react.svg?style=flat-square"></a>
|
15 | <a href="https://www.npmjs.com/package/is-react"><img src="https://img.shields.io/npm/dm/is-react.svg?style=flat-square"></a>
|
16 | <a href="https://travis-ci.org/treyhuffine/is-react"><img src="https://img.shields.io/travis/treyhuffine/is-react/master.svg?style=flat-square"></a>
|
17 | </p>
|
18 |
|
19 | A library to determine if a variable or a expression is a React element or component.
|
20 | For a more thorough understanding, this [article](https://facebook.github.io/react/blog/2015/12/18/react-components-elements-and-instances.html) describes elements and components in React, and this [article](https://facebook.github.io/react/docs/jsx-in-depth.html)
|
21 | gives an understanding of the JSX syntax.
|
22 |
|
23 | ### Sponsored by [gitconnected - The Developer Learning Community](https://gitconnected.com)
|
24 |
|
25 | ## Install
|
26 |
|
27 | `yarn add is-react` or `npm i --save is-react` to use the package.
|
28 |
|
29 | ## Examples
|
30 |
|
31 | Real world:
|
32 |
|
33 | ```javascript
|
34 | import React from 'react';
|
35 | import isReact from 'is-react';
|
36 |
|
37 | const MyImageComponent = ({ SomeProp }) => {
|
38 | if (typeof SomeProp === 'string') {
|
39 | // assume it's the src for an image
|
40 | return <img src={SomeProp} />;
|
41 | } else if (isReact.component(SomeProp)) {
|
42 | return <SomeProp />;
|
43 | } else if (isReact.element(SomeProp)) {
|
44 | return SomeProp;
|
45 | }
|
46 |
|
47 | return null;
|
48 | };
|
49 | ```
|
50 |
|
51 | ---
|
52 |
|
53 | Samples:
|
54 |
|
55 | ```javascript
|
56 | // Class Component
|
57 | class Foo extends React.Component {
|
58 | render() {
|
59 | return <h1>Hello</h1>;
|
60 | }
|
61 | }
|
62 |
|
63 | const foo = <Foo />;
|
64 |
|
65 | //Functional Component
|
66 | function Bar(props) {
|
67 | return <h1>World</h1>;
|
68 | }
|
69 | const bar = <Bar />;
|
70 |
|
71 | // React Element
|
72 | const header = <h1>Title</h1>;
|
73 |
|
74 | // Check
|
75 | isReact.compatible(Foo); // true
|
76 | isReact.component(Foo); // true
|
77 | isReact.classComponent(Foo); // true
|
78 | isReact.functionComponent(Foo); // false
|
79 | isReact.element(Foo); // false
|
80 |
|
81 | isReact.compatible(<Foo />); // true
|
82 | isReact.component(<Foo />); // false
|
83 | isReact.element(<Foo />); // true
|
84 | isReact.DOMTypeElement(<Foo />); // false
|
85 | isReact.compositeTypeElement(<Foo />); // true
|
86 |
|
87 | isReact.compatible(Bar); // true
|
88 | isReact.component(Bar); // true
|
89 | isReact.classComponent(Bar); // false
|
90 | isReact.functionComponent(Bar); // true
|
91 | isReact.element(Bar); // false
|
92 |
|
93 | isReact.compatible(<Bar />); // true
|
94 | isReact.component(<Bar />); // false
|
95 | isReact.element(<Bar />); // true
|
96 | isReact.DOMTypeElement(<Bar />); // false
|
97 | isReact.compositeTypeElement(<Bar />); // true
|
98 |
|
99 | isReact.compatible(header); // true
|
100 | isReact.component(header); // false
|
101 | isReact.element(header); // true
|
102 | isReact.DOMTypeElement(header); // true
|
103 | isReact.compositeTypeElement(header); // false
|
104 | ```
|
105 |
|
106 | ## API
|
107 |
|
108 | `import isReact from 'is-react'` to use the package
|
109 |
|
110 | All functions return a `boolean`. The primary functions you will most likely
|
111 | use are `compatible()`, `element()`, and `component()`.
|
112 |
|
113 | #### `isReact.compatible()`
|
114 |
|
115 | Determine if a variable or expression is compatible with React. Valid React
|
116 | components and elements return `true`.
|
117 |
|
118 | #### `isReact.element()`
|
119 |
|
120 | Determine if a variable or expression is a React element. Will return `true`
|
121 | for both DOM type and Composite type components.
|
122 |
|
123 | #### `isReact.component()`
|
124 |
|
125 | Determine if a variable or expression is a React component. Will return `true`
|
126 | for both functional and class components.
|
127 |
|
128 | #### `isReact.classComponent()`
|
129 |
|
130 | Determine if a variable or expression is a React class component.
|
131 |
|
132 | #### `isReact.functionComponent()`
|
133 |
|
134 | Determine if a variable or expression is a React functional component.
|
135 |
|
136 | #### `isReact.DOMTypeElement()`
|
137 |
|
138 | Determine if a variable or expression is a React DOM type element.
|
139 |
|
140 | #### `isReact.compositeTypeElement()`
|
141 |
|
142 | Determine if a variable or expression is a React Composite type element.
|
143 |
|
144 | ## Thanks!
|
145 |
|
146 | Inspired by this Stackoverflow [answer](http://stackoverflow.com/a/41658173)
|