Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【2022.9.31】setState 更新版本 #69

Open
lkzwc opened this issue Sep 1, 2022 · 0 comments
Open

【2022.9.31】setState 更新版本 #69

lkzwc opened this issue Sep 1, 2022 · 0 comments

Comments

@lkzwc
Copy link
Owner

lkzwc commented Sep 1, 2022

是同步还是异步

setState 并不是单纯同步/异步的,它的表现会因调用场景的不同而不同:在 React 钩子函数及合成事件中,它表现为异步;而在 setTimeout、setInterval 等函数中,包括在 DOM 原生事件中,它都表现为同步。这种差异,本质上是由 React 事务机制和批量更新机制的工作方式来决定的。

为什么

在react的bacthUpdates逻辑中,当更新发生时候,首先会给你全局变量附加一个批处理的值,然后执行函数,执行完之后就会将批处理标记移除,如果此时异步调用,则标记被清楚,在执行sheduleUpdateFilber就会进入同步流程,否则异步流程

react中的模式

legacy 异步 (ReactDom.render()创建的都是同步优先级)
conCurrent 都是异步

@lkzwc lkzwc changed the title 【2022.9.30】setState 更新版本 【2022.9.31】setState 更新版本 Sep 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant