如何获取 React Native 组件类的字符串名称? [英] How do I get string name of React Native component class?
本文介绍了如何获取 React Native 组件类的字符串名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式在本机反应中以字符串格式打印类名.任何人都可以让我知道,我该怎么做?
I want print class name with string format in react native programmatically. Can any one let me know that, how can I do this?
推荐答案
在 JavaScript 中,您可以使用 instance.constructor.name
获取类的名称.
In JavaScript you can get the name of the class by using instance.constructor.name
.
class Demo extends React.Component {
componentDidMount(){
console.log(this.constructor.name); // "Demo"
}
}
对于 JSX 组件,您可以使用 instance.type.displayName
.例如:
For JSX components you can use the instance.type.displayName
. For instance:
let text = <Text></Text>;
console.log(text.type.displayName) // "Text"
这篇关于如何获取 React Native 组件类的字符串名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文