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

useEffect or useLayoutEffect #40

Closed
dai-shi opened this issue Feb 13, 2020 · 6 comments
Closed

useEffect or useLayoutEffect #40

dai-shi opened this issue Feb 13, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@dai-shi
Copy link

dai-shi commented Feb 13, 2020

Let's move the discussion about this topic from #14 (comment)

@dai-shi
Copy link
Author

dai-shi commented Feb 13, 2020

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]).

@avkonst
Copy link
Owner

avkonst commented Feb 13, 2020

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

@avkonst
Copy link
Owner

avkonst commented Feb 13, 2020

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

@avkonst
Copy link
Owner

avkonst commented Feb 13, 2020

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

@dai-shi
Copy link
Author

dai-shi commented Feb 13, 2020

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.

@avkonst
Copy link
Owner

avkonst commented Feb 13, 2020

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

@avkonst avkonst added the enhancement New feature or request label Feb 13, 2020
avkonst added a commit that referenced this issue Mar 3, 2020
@avkonst avkonst closed this as completed Mar 4, 2020
shinyjohn0401 pushed a commit to shinyjohn0401/hookstate that referenced this issue Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants