Giter VIP home page Giter VIP logo

Comments (6)

lilactown avatar lilactown commented on July 25, 2024 1

Wow! Great find. Your description makes some sense to me; useEffect definitely fires after rendering is done. I can see how the race condition occurs; that's a tricky one!

I can think of two potential fixes:

1. Use useLayoutEffect instead to fire synchronously after render

However, this might have negative effects; per the React docs:

Updates scheduled inside useLayoutEffect will be flushed synchronously, before the browser has a chance to paint.

This means that when Concurrent React lands, we might be over-scheduling the state changes to re-render the component.

This actually begs the question how re-renders are being scheduled now while we're using useEffect. 🤔

2. Manually manage the subscription via refs

Something like:

(let [[v u] (<-state @a)
     atom-identity (<-ref nil)
     sub (<-ref (gensym "<-"))]
  (when (and (not= a @atom-identity)
    (when (not (nil? @atom-identity))
      (remove-watch a @sub))
    (add-watch a @sub (fn [_ _ _ v'] (u v'))))
  v)

This completely removes the act of subscribing from React's lifecycle. But once again I'm not sure how this interacts with React's scheduler.

I might reach out to the React team to see the best way to handle this case. I imagine this would come up anytime you're subscribing to some external event bus.

from hx.

lilactown avatar lilactown commented on July 25, 2024 1

Looks like there is already an issue in the React repo: facebook/react#14988

gaeron posted this:

As for fixing the original issue, the strategy is to read the mutable value in useEffect. If it changed since the value captured during render, setState.

Which seems like an easy fix.

from hx.

orestis avatar orestis commented on July 25, 2024

from hx.

lilactown avatar lilactown commented on July 25, 2024

Would you be able to verify that 6d3b7cb fixes the issue?

from hx.

orestis avatar orestis commented on July 25, 2024

I’ll have to test it on Monday, but I applied pretty much the same code (sans comparison) in a local version and that did the trick. The comparison should be ok, since there can’t be a second thread mutating the atom behind the scenes.

Thanks!

from hx.

lilactown avatar lilactown commented on July 25, 2024

Cool. Closing this for now. Re-open if you still run into issues.

from hx.

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.