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

Update usestate.md #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/hooks/usestate.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ if (fiber.lanes === NoLanes && (alternate === null || alternate.lanes === NoLane

`fiber.lanes === NoLanes`意味着`fiber`上不存在`update`。

我们已经知道,通过`update`计算`state`发生在`申明阶段`,这是因为该`hook`上可能存在多个不同`优先级`的`update`,最终`state`的值由多个`update`共同决定。
我们已经知道,通过`update`计算`state`发生在`声明阶段`,这是因为该`hook`上可能存在多个不同`优先级`的`update`,最终`state`的值由多个`update`共同决定。

但是当`fiber`上不存在`update`,则`调用阶段`创建的`update`为该`hook`上第一个`update`,在`申明阶段`计算`state`时也只依赖于该`update`,完全不需要进入`申明阶段`再计算`state`。
但是当`fiber`上不存在`update`,则`调用阶段`创建的`update`为该`hook`上第一个`update`,在`声明阶段`计算`state`时也只依赖于该`update`,完全不需要进入`声明阶段`再计算`state`。

这样做的好处是:如果计算出的`state`与该`hook`之前保存的`state`一致,那么完全不需要开启一次调度。即使计算出的`state`与该`hook`之前保存的`state`不一致,在`申明阶段`也可以直接使用`调用阶段`已经计算出的`state`。
这样做的好处是:如果计算出的`state`与该`hook`之前保存的`state`一致,那么完全不需要开启一次调度。即使计算出的`state`与该`hook`之前保存的`state`不一致,在`声明阶段`也可以直接使用`调用阶段`已经计算出的`state`。

> 你可以在[这里](https://github.com/acdlite/react/blob/1fb18e22ae66fdb1dc127347e169e73948778e5a/packages/react-reconciler/src/ReactFiberHooks.new.js#L1727)看到这段提前计算`state`的逻辑

Expand Down