如何检测React组件与React元素? [英] How to detect a React component vs. a React element?
本文介绍了如何检测React组件与React元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
React.isValidElement
对React组件和React元素都测试为true。具体来说,我如何测试对象是否为React组件?目前,我是通过测试 typeof obj.type ==='function'
来实现的,但我希望有更好的方法。
React.isValidElement
tests true for both React components as well as React elements. How would I test, specifically, that an object is a React component? Currently, I'm doing it by testing typeof obj.type === 'function'
, but I'm hoping there's a better way.
推荐答案
ReactComponent.prototype.isReactComponent = {};
33 of /node_modules/react/lib/ReactComponent.js
使用npm安装。此时,没有可用于检查其有效性的直接方法。你在做什么是正确的。
33 of /node_modules/react/lib/ReactComponent.js
Install using npm. At this point, there is no direct method available to check for its validity. What you are doing is correct.
这篇关于如何检测React组件与React元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文