【发布时间】:2015-11-25 20:04:53
【问题描述】:
我收到以下错误
未捕获的类型错误:无法读取未定义的属性“setState”
即使在构造函数中绑定了 delta。
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 1
};
this.delta.bind(this);
}
delta() {
this.setState({
count : this.state.count++
});
}
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={this.delta}>+</button>
</div>
);
}
}
【问题讨论】:
-
在 ES6 中,使用 can you use arrow function 进行函数声明来解决这个问题。
-
^ 这应该是正确答案
-
我把我的响应函数改成了 ES6,而且,它的工作原理很好。
标签: javascript reactjs