1,通常我们使用setState,会传入一个对象,这是对象式的setState。
const {count} = this.state
this.setState({count:count+1})
2,然而setState还有另外一种写法,就是函数式的setState。
this.setState( state => ({count:state.count+1}))
实际上,对象式的setState是函数式的setState的简写方式(语法糖)。
所以如果你的setState比较复杂,可以选择函数式。
3,因为setState的动作是异步的,所以setState还有第二个参数,回调函数。
this.setState({count:count+1},()=>{
console.log(this.state.count);
})
如果要查看更新后的状态,需要在回调函数中查看。