Comments (8)
In any case, I don't think I'm going to take time explaining this other than to say "don't do this" 😅
Thanks!
from advanced-react-hooks.
I think the reason that you're hitting dead ends on finding information here is because nobody expected that people would do that sort of thing. I'm not sure why you would.
What I suggest is you publish your findings in a blog post :) I could even reference it in the workshop material if you don't mind!
from advanced-react-hooks.
Not only necessary but also one where this the natural way to solve the problem
from advanced-react-hooks.
That's still contrived. I'm not sure why you'd ever need to display that value to a user 🤷♂️
from advanced-react-hooks.
"nobody expected that people would do that sort of thing"
Why not? It's not explicitly nor implicitly stated anywhere in React docs not to do this. 😢
A legitimate use case for updating state in useLayoutEffect is when both are true:
- The state update depends on the updated DOM for that render.
- It must be before paint.
My understanding is that if you have those requirements, the state update must be done in useLayoutEffect. But as I mentioned before, it comes with an undocumented and unintuitive behavior that may:
- cause unexpected effects for devs (as seen by that github issue)
- tank performance
TLDR; I'm not sure saying "just don't do this" is the way to go, since it seems like there are legitimate use cases. And seeing how this course is geared toward intermediate/advanced React developers, this kind of nitty gritty detail is exactly what I love. 😄
You may be right that this may be more suitable for a blog post though. I don't have time to create one at the moment, but feel free to make use of this discussion if you're ever inclined. ❤️
Anyway, really enjoying the content, Kent! Look forward to learning more.
from advanced-react-hooks.
Sorry, I don't understand the use case. Could you describe an actual use case where this would be necessary?
from advanced-react-hooks.
Sure, I made an example that demonstrates the use case I mentioned. Please toggle between useEffect vs useLayoutEffect and see the UI lag difference after clicking "remove message".
Note that useLayoutEffect is required here due to <SlooooowSibling />. Per your blogpost, one should use useEffect if "your DOM changes are unobservable". Unfortunately, DOM changes are indeed observable here. I believe this satisfies the "necessary" part. As to "natural"... Given a <SlooooowSibling />, I'm not sure if there's a more "natural" way to solve this use case. 😅
If there wasn't a <SlooooowSibling />, then you should definitely use useEffect, because as I previously mentioned, setting state in useLayoutEffect is very nonperformant. Having to use useLayoutEffect in my use case above actually comes with heavy costs. Please let me know if there's a better solution. 👍
from advanced-react-hooks.
Fair enough. 😥
from advanced-react-hooks.
Related Issues (20)
- Start the TypeScript-fication process by creating a `ts/main` branch HOT 6
- Warning not triggering from exercise 2, extra credit 3 HOT 1
- edit
- Another potential exercise 2 extra credit HOT 2
- Tests doesn't pass for `extra-1` in exercise 6 HOT 1
- Broken reference link in the documentation HOT 1
- Broken link to `How to contribute to open source`
- React team has removed the warning on setting `setState` on an unmounted component HOT 5
- Changelog HOT 1
- useContext gotchas HOT 1
- React DevTools only works on isolated page HOT 3
- Node 17 HOT 1
- Exercise 4 (useLayoutEffect: auto-scrolling textarea) -> no discernible difference between useEffect and useLayoutEffect in React 18
- useLayoutEffect: auto-scrolling textarea HOT 1
- Docker compose for advanced-react-hooks fails HOT 4
- useDebugValue - production deploys don't show anything HOT 2
- Please tell why did you say its still valuable to apply useSafeDispatch() function in your material even after react 18? HOT 1
- useDebugValue: useMedia (Advanced React Hooks) HOT 2
- [docs] Update repo docs pointing to react old link to new link.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from advanced-react-hooks.