在 React.createClass 中调用其他函数



我正在尝试调用React.createClass函数中的其他函数,但由于某种原因它不起作用,我得到了Cannot read property 'test' of undefined

var WallFeed = React.createClass({
test: () => {
console.log('hello world');
},
componentDidMount: () => {
this.test();
},
render: () => {
return (
<div>test</div>
);
}
});

现在,我如何从 componentDidMount 调用 this.test(( (React's build in function( ?

谢谢!

不要在传递给createClass的对象文本中使用箭头函数。对象文本中的箭头函数将始终以window作为this调用。有关说明,请参阅对象文字中的箭头函数:

var WallFeed = React.createClass({
test() {
console.log('hello world');
},
componentDidMount() {
this.test();
},
render() {
return (
<div>test</div>
);
}
});

最新更新