【发布时间】:2018-11-04 06:21:33
【问题描述】:
这是我的代码:
import React, { Component } from 'react';
class Counter extends Component {
constructor(props){
super(props);
this.state = {
number : 0
};
this.handleIncrease = this.handleIncrease.bind(this);
this.handleDecrease = this.handleDecrease.bind(this);
}
handleIncrease = () => {
this.setState=({
number: this.state.number + 1
});
console.log("handleIncrease()");
}
handleDecrease = () => {
this.setState({
number: this.state.number - 1
});
console.log("handleDecrease()");
}
render(){
return (
<div>
<hr/>
<h1>Counter</h1>
<p>number : {this.state.number}</p>
<button onClick={this.handleIncrease}>+</button>
<button onClick={this.handleDecrease}>-</button>
<hr/>
</div>
)
}
}
export default Counter;
当我按下 - 按钮时它可以工作,但当我按下 + 按钮时它不起作用并出现错误:
TypeError: _this.setState 不是函数 Counter._this.handleDecrease
【问题讨论】:
-
this.setState=(,在handleIncrease函数中删除=之前的(。 -
谢谢!!!!非常!!!!!!!!!!!!! !!!!!!!!!!!!哇!!!!!!!!!我傻!!!
标签: javascript reactjs setstate