reactjs - React 功能组件 - 测试一个元素 instanceOf baseElement
问题描述
在旧的方式反应组件(基于类的组件)中,
很容易确定指定的子/元素是 baseComponent 的类型。
像这样的东西:
class CatComponent extends React.Component { /* ... */ }
class LionComponent extends CatComponent { /* ... */ }
/* ... */
<FooComponent>
{(Array.isArray(props.children) ? props.children : [props.children]).map((child, index) => {
if (React.isValidElement(child) && (child.type.prototype instanceof CatComponent || child.type===CatComponent)) {
return doSomething(child);
}
else {
return doAnother(child);
}
})}
</FooComponent>
/* ... */
问题是,像这样的功能组件怎么样:
function CatComponent() { /* ... */ }
function LionComponent() { return <CatComponent>
<AnotherComp>
</AnotherComp>
</CatComponent> }
有没有办法确定LionComponent
是那种CatComponent
?
child.type
指向但LionComponent
我不知道确定LionComponent
是那种CatComponent
。
解决方案
推荐阅读
- excel - EXCEL VBA Specialcells 读取行
- react-native - React native Expo TypeError:网络请求失败
- r - 重命名R中列表列表中的列
- upgrade - 我正在使用快速 cgi 将 PHP 5.6 升级到 7.2。安装后出现内部服务器错误
- express - sequelize 创建数据库记录后如何从响应中删除 json 元素?
- python - 使用海龟和单击/双击实现 Python tkinter 缩放
- mysql - 在两个查询中计算
- apache-spark - Spark 与 Dask 中的容错
- java - 遇到 SkipException、testNG 问题,我做错了什么?
- java - 使用 ArrayList 进行用户输入