如何在 this.props.children 中访问 React 对象的类名 [英] How to access the class name of a React object in this.props.children

查看:35
本文介绍了如何在 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 对象的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆