如何在 this.props.children 中访问 React 对象的类名

编程入门 行业动态 更新时间:2024-10-24 12:31:43
本文介绍了如何在 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 对象的类名

本文发布于:2023-10-04 04:33:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   如何在   children   props   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!