Open chunbin1 opened 3 years ago
改成setTimeout
class App extends React.Component{
state = { number:0 }
componentDidMount = ()=>{
setTimeout(()=>{
this.setState({ number:this.state.number + 1 },()=>{ console.log( 'callback1', this.state.number) })
console.log(this.state.number)
this.setState({ number:this.state.number + 1 },()=>{ console.log( 'callback2', this.state.number) })
console.log(this.state.number)
this.setState({ number:this.state.number + 1 },()=>{ console.log( 'callback3', this.state.number) })
console.log(this.state.number)
})
}
render(){
return <div>
{ this.state.number }
</div>
}
}
输出:callback1 1, 1, callback2 2, 2, callback 3, 3
ReactDOM.unstable_batchedUpdates
isBatchingEventUpdates
控制是否批量更新。
在事件和生命周期中,会合并更新
求输出结果
输出: 0 0 0 callbackl1 1 callback2 1 callback3 1