gpt4 book ai didi

reactjs - 检查 React 组件的类型

转载 作者:行者123 更新时间:2023-12-03 13:25:59 25 4
gpt4 key购买 nike

我需要循环遍历组件的children,并且仅当子组件具有特定类型时才执行一些操作:

React.Children.forEach(this.props.children, child => {
if (...) {
console.log('Suitable component!');
}
});

最佳答案

这是你应该做的:

import MyComponent from './MyComponent';

this.props.children.forEach(child => {
if (child.type === MyComponent) {
console.log('This child is <MyComponent />');
}
});

关于reactjs - 检查 React 组件的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55729582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com