反应,setState 在我的反应应用程序中不起作用
react, setState does not work in my react app
这是我的代码:
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 is not a function
Counter._this.handleDecrease
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 = () => {
//here remove equal
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;
去掉等号:
handleIncrease = () => {
>>>> this.setState({
number: this.state.number + 1
});
console.log("handleIncrease()");
}
此外,当使用先前的状态设置新状态时,在 setState 中传递一个函数,因为状态更新可以是异步的。
这是我的代码:
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 is not a function Counter._this.handleDecrease
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 = () => {
//here remove equal
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;
去掉等号:
handleIncrease = () => {
>>>> this.setState({
number: this.state.number + 1
});
console.log("handleIncrease()");
}
此外,当使用先前的状态设置新状态时,在 setState 中传递一个函数,因为状态更新可以是异步的。