this.setState
handleClick() {
this.setState(function (state) {
return {
// You should never mutate the state
// count: state.count++, // equal to -> count: this.state.count = this.state.count + 1
count: state.count + 1 // recommend
}
}
}
let num = 1
let obj = {
// 合法的操作
count: num++, // equal to -> count: (num = num + 1)
}
obj // {count: 2}
https://stackoverflow.com/questions/39316376/how-to-use-the-increment-operator-in-react
https://reactjs.org/docs/react-component.html#setstate
网友评论