使用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