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

(fix) - set refs in effect #418

Merged
merged 6 commits into from
Sep 6, 2019
Merged

(fix) - set refs in effect #418

merged 6 commits into from
Sep 6, 2019

Conversation

JoviDeCroock
Copy link
Collaborator

@JoviDeCroock JoviDeCroock commented Sep 6, 2019

Looking at the code this made me realise that the WILL_MOUNT and DID_MOUNT are two and the same. We just want the effect to execute and the only way that's possible is just to rely on not switching to UPDATE.

This solution is derived from the twitter conversation on twitter about concurrent React. We can't rely on any other things in the if-clause this MAY cause a potential second fetch due to the process being interrupted before we can actually set the ref.current to UPDATE. I think this is something that is acceptable and is maybe more an issue with the concept of concurrent than anything else. The second operation dispatch will normally hit the cache anyway and cause an instant return.

I was initially thinking about making the clause

  if (state.current === WILL_MOUNT && !teardown.current) {
    state.current = DID_MOUNT;
    teardown.current = effect();
  }

But this would be subject to the mistake of relying on a ref set during render (outside of an effect again). I can't reproduce the issue itself, that's the only problem I'm actually having.

I've tested this code by using unstable_createRoot in our first example, this results in nothing visually different from the previous approach. An article I found said to try StrictMode since this invokes every render twice but this also does not seem to produce the issue.

In all honesty we trigger a side-effect in render now and maybe that's something we'd try to avoid with concurrent react. I know that it isn't the ideal thing but we'd ensure compatibility.

I hope this all makes sense.

Attempts to fix: #342

Source: https://frontarm.com/daishi-kato/use-ref-in-concurrent-mode/

…ect, this entails the potential danger that we dispatch two network requests.
@JoviDeCroock JoviDeCroock requested a review from kitten September 6, 2019 09:41
Copy link
Member

@kitten kitten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tests are still passing and this seems to fulfil the requirement of not altering the ref outside of the effect 👍

@JoviDeCroock JoviDeCroock merged commit 5102c6c into master Sep 6, 2019
@JoviDeCroock JoviDeCroock deleted the fix/useImmediateEffect branch September 6, 2019 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition resolving queries with React's concurrent mode
2 participants