1 | react |
的
1 | setState |
是异步的,这样可以批量更新,不至于每次
1 | setState |
都进行新的
1 | render |
。
但是在调试代码时发现,在
1 | componentDidMount |
中多次进行
1 | setState |
,结果也是批量的,但是
1 | isBatchingUpdates |
却为
1 | false |
;
而在事件中触发的
1 | setState |
,
1 | isBatchingUpdates |
却是为
1 | true |
。
这是为什么呢,为什么生命周期中的没使用批量更新标志,它是如何实现的呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | componentDidMount() { this.setState({ count: this.state.count + 1 }); console.log(this.state.count); this.setState({ count: this.state.count + 1 }); console.log(this.state.count); } OnClickBtn= () => { this.setState({ count: this.state.count + 1 }); console.log(this.state.count); this.setState({ count: this.state.count + 1 }); console.log(this.state.count); } render() { console.log("render", this.state.count); return (
); } |