UNPKG

4.18 kBMarkdownView Raw
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
19A library to determine if a variable or a expression is a React element or component.
20For 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)
21gives 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
31Real world:
32
33```javascript
34import React from 'react';
35import isReact from 'is-react';
36
37const 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
53Samples:
54
55```javascript
56// Class Component
57class Foo extends React.Component {
58 render() {
59 return <h1>Hello</h1>;
60 }
61}
62
63const foo = <Foo />;
64
65//Functional Component
66function Bar(props) {
67 return <h1>World</h1>;
68}
69const bar = <Bar />;
70
71// React Element
72const header = <h1>Title</h1>;
73
74// Check
75isReact.compatible(Foo); // true
76isReact.component(Foo); // true
77isReact.classComponent(Foo); // true
78isReact.functionComponent(Foo); // false
79isReact.element(Foo); // false
80
81isReact.compatible(<Foo />); // true
82isReact.component(<Foo />); // false
83isReact.element(<Foo />); // true
84isReact.DOMTypeElement(<Foo />); // false
85isReact.compositeTypeElement(<Foo />); // true
86
87isReact.compatible(Bar); // true
88isReact.component(Bar); // true
89isReact.classComponent(Bar); // false
90isReact.functionComponent(Bar); // true
91isReact.element(Bar); // false
92
93isReact.compatible(<Bar />); // true
94isReact.component(<Bar />); // false
95isReact.element(<Bar />); // true
96isReact.DOMTypeElement(<Bar />); // false
97isReact.compositeTypeElement(<Bar />); // true
98
99isReact.compatible(header); // true
100isReact.component(header); // false
101isReact.element(header); // true
102isReact.DOMTypeElement(header); // true
103isReact.compositeTypeElement(header); // false
104```
105
106## API
107
108`import isReact from 'is-react'` to use the package
109
110All functions return a `boolean`. The primary functions you will most likely
111use are `compatible()`, `element()`, and `component()`.
112
113#### `isReact.compatible()`
114
115Determine if a variable or expression is compatible with React. Valid React
116components and elements return `true`.
117
118#### `isReact.element()`
119
120Determine if a variable or expression is a React element. Will return `true`
121for both DOM type and Composite type components.
122
123#### `isReact.component()`
124
125Determine if a variable or expression is a React component. Will return `true`
126for both functional and class components.
127
128#### `isReact.classComponent()`
129
130Determine if a variable or expression is a React class component.
131
132#### `isReact.functionComponent()`
133
134Determine if a variable or expression is a React functional component.
135
136#### `isReact.DOMTypeElement()`
137
138Determine if a variable or expression is a React DOM type element.
139
140#### `isReact.compositeTypeElement()`
141
142Determine if a variable or expression is a React Composite type element.
143
144## Thanks!
145
146Inspired by this Stackoverflow [answer](http://stackoverflow.com/a/41658173)