我得到以下错误
无法读取undefined的属性“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>
);
}
}
检查状态
检查状态是否创建特定属性
这一点。状态= {
名称:",
电子邮件:”“
}
this.setState(() => ({
Comments: Comments //注释状态不可用
}))
2.检查(这个)
如果你在任何函数(即handleChange)中执行setState,检查函数是否绑定到这个函数或函数应该是箭头函数。
## 3种方法绑定到下面的函数##
//3 ways for binding this to the below function
handleNameChange(e) {
this.setState(() => ({ name }))
}
// 1.Bind while callling function
onChange={this.handleNameChange.bind(this)}
//2.make it as arrow function
handleNameChange((e)=> {
this.setState(() => ({ name }))
})
//3.Bind in constuctor
constructor(props) {
super(props)
this.state = {
name: "",
email: ""
}
this.handleNameChange = this.handleNameChange.bind(this)
}
检查状态
检查状态是否创建特定属性
这一点。状态= {
名称:",
电子邮件:”“
}
this.setState(() => ({
Comments: Comments //注释状态不可用
}))
2.检查(这个)
如果你在任何函数(即handleChange)中执行setState,检查函数是否绑定到这个函数或函数应该是箭头函数。
## 3种方法绑定到下面的函数##
//3 ways for binding this to the below function
handleNameChange(e) {
this.setState(() => ({ name }))
}
// 1.Bind while callling function
onChange={this.handleNameChange.bind(this)}
//2.make it as arrow function
handleNameChange((e)=> {
this.setState(() => ({ name }))
})
//3.Bind in constuctor
constructor(props) {
super(props)
this.state = {
name: "",
email: ""
}
this.handleNameChange = this.handleNameChange.bind(this)
}