7kms/react-illustration-series

executionContext 何时为空?

DaphnisLi opened this issue · 2 comments

文章中提到executionContext执行栈为空的时候,会取消 schedule 调度, 主动刷新回调队列flushSyncCallbackQueue()

那 executionContext 都在什么情况下为空呢?

7kms commented

在react自身上下文之外触发更新时, executionContext就为空.

正常情况下的节点更新都是在合成事件的回调函数中,进行setState. 在执行这个回调之前, react已经设置好了executionContext

如果绕过react内部设置executionContext的时机, 比如在setTimeout的回调, 原生事件的回调, Promise.resolve回调等中去执行setState, 这个回调函数是js直接运行, 没有通过react内核来运行, 所以react没有机会设置executionContext, 此时就为空.

在react自身时间之外触发更新的时候,executionContext就为空。

正常情况下,都是在合成事件的微不足道的功能中,进行的setState。在执行这个之前,react 已经设置好了executionContext

如果此时再把这个动作设置为内部设置executionContext的时机是直接的,setTimeout的回调, 原生事件的回调, Promise.resolve回调等中去执行设置,直接运行运行,没有通过react内核来,所以react没有机会设置executionContext,就为直接js运行。

哈哈,学习到了,这个解答太好了