react面试题——理解setState(源码object.assign)

setState是异步的方式
this.setState({
    counter:this.state.counter+1
})
console.log(this.state.counter)
setState是异步,执行最后一个setState
同步执行的方法,传递函数
1,this.setState(nextState=>{
    return{
       counter: nextState.counter+1
    }
})
2,setTimeout(()=>{
    this.changeValue()
},1000)
3,
this.setState({
    counter:this.state.counter+1
},()=>{
    console.log(2)
})
4,原生事件
 doccument.body.addEventListener('click',this.changeValue,false)
注释
setState只有在合成事件的和生命周期才是异步,原生事件好的setTimeout是同步的,这里的异步的是批量更新
原文地址:https://www.cnblogs.com/yayaxuping/p/11972346.html