在回调中,我希望将道具传递给组件,但无法通过this.props
,因为this
未定义。ReactJS在回调中访问“this”
下面是一个简单的例子:
var MyComponent = React.createClass({
options:{
componentFunction: function(c) {
console.log(this.props.myProp); //this references to the options here, not the component itself
}
},
render: function() {
return (
<OtherComponent options={ this.options } />
);
}
});
而且我通过道具是这样的:
<MyComponent myProp={"x"};
希望得到任何帮助, 感谢。
我强烈建议迁移到ES6语法。使用箭头函数,这将是没有问题的。 –
感谢您的建议,将为我的下一个项目做! – Surreal