在react组件渲染方法中,此this.props.children中具有一些子组件。如何获得每个 child 的组件(类)名称以区分它们?
React.Children.map(this.props.children, function(child){
// how can I get the class name of a child or some other identifier
})
最佳答案
我认为您想要的是child._currentElement.type.displayName
,但我会谨慎使用这样的内部结构,除非您要仔细检查它们在每次升级中仍能正常运行。我认为没有公开的API可以获取组件的名称。
关于javascript - 如何在this.props.children中访问React对象的类名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28218814/