Skip to content

Commit f5d57ee

Browse files
authored
Merge pull request #982 from gaearon/better-type-comparison-fix
docs(readme): Better type comparison
2 parents 352e61b + 769a2e8 commit f5d57ee

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,34 @@ Another way - compare "rendered" element type
271271

272272
```js
273273
const element = <Component />
274-
console.log(element.type === (<Component/>).type) // true
274+
console.log(element.type === <Component />.type) // true
275275

276276
// better - precache rendered type
277277
const element = <Component />
278-
const ComponentType = (<Component />).type
279-
console.log(element.type === ComponentType // true
278+
const ComponentType = <Component />.type
279+
console.log(element.type === ComponentType) // true
280280
```
281281

282+
But you might have to provide all required props. See [original issue](https://github.com/gaearon/react-hot-loader/issues/304).
283+
This is most reliable way to compare components, but it will not work with required props.
284+
282285
Another way - compare Component name.
283-
> Not all components has a name
286+
287+
> Not all components has a name. **In production displayName could not exists.**
288+
289+
```js
290+
const element = <Component />
291+
console.log(element.displayName === 'Component') // true
292+
```
293+
294+
For Components you might be able to use **instanceof** operator
295+
284296
```js
285297
const element = <Component />
286-
console.log(element.displayName === "Component") // true
298+
console.log(element.type instanceof Component) // true
287299
```
288300

301+
This is something we did not solve yet.
289302

290303
### Webpack ExtractTextPlugin
291304

0 commit comments

Comments
 (0)