GithubHelp home page GithubHelp logo

useEffect or useLayoutEffect about hookstate HOT 6 CLOSED

avkonst avatar avkonst commented on August 22, 2024
useEffect or useLayoutEffect

from hookstate.

Comments (6)

dai-shi avatar dai-shi commented on August 22, 2024

Well, I actually wanted to use useEffect everywhere and drop useLayoutEffect.

Yeah, we should always prefer using useEffect.

wondered how "safe" it would be for Hookstate

I'm not sure as I don't look into the recent hookstate code.
Updating refs in useEffect is probably fine for Blocking Mode.
The reason I use useLayoutEffect in react-tracked is for Concurrent Mode. (I have two useLayoutEffects for different reasons: a) to update refs and b) to subscribe. a) is the same in react-redux. b) is for my specific hack in my three libs [more info about the hack]).

from hookstate.

avkonst avatar avkonst commented on August 22, 2024

basically, a user interaction or network event CAN NOT TRIGGER any state change BEFORE useEffect is completed but AFTER dom drawing is completed?

from hookstate.

avkonst avatar avkonst commented on August 22, 2024

@dai-shi right or no? what do you think?

from hookstate.

avkonst avatar avkonst commented on August 22, 2024

for reference for the future: https://twitter.com/dai_shi/status/1227237850707447809

from hookstate.

dai-shi avatar dai-shi commented on August 22, 2024

https://reactjs.org/docs/hooks-reference.html#useeffect

Although useEffect is deferred until after the browser has painted, it’s guaranteed to fire before any new renders. React will always flush a previous render’s effects before starting a new update.

You can trigger state updates before effects, but it will not render.
So, React "state" won't change, but any other JS variables can change.

Note in CM, it can render multiple times before effect runs, so you can't reliably mutate variables in render functions.

from hookstate.

avkonst avatar avkonst commented on August 22, 2024

Nice thanks. It means I can boost the performance a little bit more

from hookstate.

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.