GithubHelp home page GithubHelp logo

Comments (8)

kentcdodds avatar kentcdodds commented on May 17, 2024 2

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.

kentcdodds avatar kentcdodds commented on May 17, 2024 1

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.

kentcdodds avatar kentcdodds commented on May 17, 2024 1

Not only necessary but also one where this the natural way to solve the problem

from advanced-react-hooks.

kentcdodds avatar kentcdodds commented on May 17, 2024 1

That's still contrived. I'm not sure why you'd ever need to display that value to a user 🤷‍♂️

from advanced-react-hooks.

bruce-c-liu avatar bruce-c-liu commented on May 17, 2024

"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:

  1. The state update depends on the updated DOM for that render.
  2. 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:

  1. cause unexpected effects for devs (as seen by that github issue)
  2. 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.

kentcdodds avatar kentcdodds commented on May 17, 2024

Sorry, I don't understand the use case. Could you describe an actual use case where this would be necessary?

from advanced-react-hooks.

bruce-c-liu avatar bruce-c-liu commented on May 17, 2024

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.

bruce-c-liu avatar bruce-c-liu commented on May 17, 2024

Fair enough. 😥

from advanced-react-hooks.

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.