令人迷惑的setState
在react18之前,setState 对state的更新时机确实是存在同步和异步两种情况,对用户来说这是个糟糕的设计。
大概是这样:
- 在新创建的宏任务中,如setTimeout、事件监听的回调函数中,setState是同步更新state的
- 其他情况,是异步的
在react18,使用createRoot,在实践中暂时没发现有同步的情况,都是异步的。后面发现新情况再补充。
但是有个疑惑,先看如下代码
React v18.2.0 ,注意react和react-dom 都是18.2.0
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
// 注意这里用了createRoot API
const root = ReactDOM.createRoot(document.getElementById('root'))
root.render(
<App />
)
import React from "react";
class Test extends React.Component {
state = {
count: 0,
};
componentDidMount() {
this.setState({
count: this.state.count + 1,
});
console.log('count: ', this.state.count) // 0
Promise.resolve().then(() => {
// 这里可以拿到新的值
console.log('count: ', this.state.count) // 1
})
setTimeout(() => {
console.log('count: ', this.state.count) // 1
this.setState({
count: this.state.count + 1,
});
console.log('count: ', this.state.count) // 1
Promise.resolve().then(() => {
// 看这里,为什么这里没有拿到新的值?
console.log('count: ', this.state.count) // 1 why?
})
setTimeout(() =>{
console.log('count: ', this.state.count) // 2
}, 0)
}, 0)
}
}
setState更新state的时机,一共分几种情况?