【发布时间】:2016-02-08 12:48:13
【问题描述】:
在使用 ES6 和 react 0.14 时,有没有办法避免样板?
到目前为止,我不必担心我的函数会绑定到我创建的Component,但这不再是(为什么?!?)这种情况,组件只绑定到Component super类(如果我正确理解了错误)。
所以我每次创建新类时真正需要做的就是将这段代码添加到构造函数中:
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,它将失败(没有道具、状态或任何东西)。
有没有办法解决这个问题?
【问题讨论】:
标签: reactjs