我想要以字符串格式打印类名,以编程方式进行本机反应。谁能告诉我,我该怎么做?
在 JavaScript 中,您可以使用以下方式获取类的名称instance.constructor.name
.
class Demo extends React.Component {
componentDidMount(){
console.log(this.constructor.name); // "Demo"
}
}
对于 JSX 组件,您可以使用instance.type.displayName
。例如:
let text = <Text></Text>;
console.log(text.type.displayName) // "Text"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)