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

React Hooks - useEffect/useLayout #52

Open
Tracked by #51
imfenghuang opened this issue Jul 5, 2024 · 0 comments
Open
Tracked by #51

React Hooks - useEffect/useLayout #52

imfenghuang opened this issue Jul 5, 2024 · 0 comments

Comments

@imfenghuang
Copy link
Owner

imfenghuang commented Jul 5, 2024

function Foo() {

  useEffect(() => {
    console.log(1);
  }, []);

  useLayoutEffect(() => {
    console.log(2);
  }, []);

  useEffect(() => {
    console.log(3);
  }, []);

  return <p>this is Foo</p>;
}

按上述示例,挂载相关副作用 hook 后,内部逻辑结构如下:

  1. Foo 的 fiber.memoizedState 指向第一个 useEffect hook,该 hook.memoizedState 指向一个 effct 对象,该对象的 next 指向 useLayoutEffect hookeffect 对象;

  2. 指向第一个 useEffect hookhook.next 指向下一个 useLayoutEffect hook

  3. 最后一个 useEffect hookhook.memoizedStateeffctnext 指向第一个 useEffect hookeffct

  4. Foo 的 fiber.updateQueue.lastEffect 指向最后一个 useEffect hookeffect 对象;

  5. 不同类型的 effect.tag(HookFlags)也不同:

    • useEffct tag 有 HasEffectPassive
    • useLayoutEffect tag 有 HasEffectLayout

React Hooks - effect svg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant