Coder Social home page Coder Social logo

Comments (1)

wangfengyuan avatar wangfengyuan commented on May 27, 2024

不太会,跑了下,运行结果如下
https://stackblitz.com/edit/vitejs-vite-ibcjhf?file=src%2FApp.jsx

App: render
Child: Render
Child: useLayoutEffect
App: useLayoutEffect
Child: useEffect
App: useEffect

学习了下面两篇文章:
https://jser.dev/2023-07-08-how-does-useeffect-work
https://jser.dev/react/2021/12/04/how-does-useLayoutEffect-work

总结一下我的理解:
1、useEffect 通过 scheduleCallback调度的,是异步执行的,也就是在渲染到页面后执行
而useLayoutEffect 是同步执行的,发生在dom mutation更新了dom结构,但是还未绘制到屏幕之前
2、useEffect 和 useLayoutEffect 都是递归执行的,先执行子组件
3、有useEffect 和 useLayoutEffect的fiber会被打上标记,加入到effectList中, 每次更新都会都会处理, 两个函数都会处理成effectObject, 包含create、destory属性,其中create是useEffect 和 useLayoutEffect传入的函数,destory对应传入的函数执行返回的函数,在commit阶段,每次都是先执行destory清理函数,然后执行create, 挂载时destory为undefined,跳过清理函数执行,执行create,执行后把return的函数复制给destory, 下一次更新时destory不为undefined就会执行destory销毁函数,如果dep有变化接下来执行create

from daily-question.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.