如果给定实例,我可以检查React组件类的原型吗? [英] Can I inspect the proptypes of react component class if given an instance?
本文介绍了如果给定实例,我可以检查React组件类的原型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很想访问一个类的propTypes,例如
I'd love to access the propTypes of a class, e.g.
var Button = React.createClass({
propTypes: {
text: React.PropTypes.string,
href: React.PropTypes.string
},
...
})
...
var ButtonPropTypes = Button.propTypes
我希望看到类似的东西
{ text: function(){...}, href: function(){...} }
但是我得到了undefined
.我在做错什么,如何获得原型?
But instead I get undefined
. What am I doing wrong, and how can I get at the proptypes?
推荐答案
您应该能够像在代码中一样静态地访问原型.我在jsfiddle上举了一个例子,您应该能够看到控制台中正在打印的原型(它们是函数)
You should be able to access the proptypes statically as you are doing in your code. I put an example on jsfiddle and you can should be able to see the proptypes being printed in the console (they are functions)
var Hello = React.createClass({
getDefaultProps: function() {
return {
testing: 'default value'
};
},
propTypes: {
testing: React.PropTypes.string
},
render: function() {
return (
<div>
<div>{this.props.name}</div>
</div>
)
}
});
React.render(<Hello name="World" />, document.getElementById('container'));
console.log(Hello.propTypes)
https://jsfiddle.net/fj7ax0qr/
这篇关于如果给定实例,我可以检查React组件类的原型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文