本文介绍了如何在 this.props.children 中访问 React 对象的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 this.props.children 中有一些子组件的 react 组件渲染方法中.如何获取每个孩子的组件(类)名称以区分它们?
In a react component render method that has some children components in this.props.children. How can I get the component (class) name of each child to differentiate between them?
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.
I think what you want is child._currentElement.type.displayName but I would be careful about using internals like this, unless you want to double check they still function correctly in every upgrade. I don't think there is a public api for getting the name of a component.
更多推荐
如何在 this.props.children 中访问 React 对象的类名
发布评论