使用ES6时反应0.14是否有避免样板的方法?React仅将组件方法绑定到此 - 解决方法?
到现在为止,我不必担心我的函数被绑定到我创建的Component
,但这不再是(为什么?!?)的情况和Component仅限于Component
超类(If我正确理解错误)。
所以我真的需要每个创建一个新的类就是这个代码添加到构造时间做:
class CustomComp extends React.Component {
constructor() {
super();
this.newFunction = this.newFunction.bind(this);
}
newFunction(){
console.log('This is user defined function');
}
render() {
return <button onClick={this.newFunction}>Click</button>
}
}
所以,如果我不会绑定newFunction
它会失败(无道具,状态或任何)。
有没有办法解决这个问题?
https://medium.com/@john1jan/react-binding-revealed-aa458df8c136#.fd5z0vmjl – John