Open idubrov opened 3 years ago
Please note that calling setSelf()
from window.requestAnimationFrame()
will first initialize the atom to the default value, then asynchronously update the value. Calling setSelf()
synchronously in the effect will initialize that atom to that value for the first render, which is important for things like SSR and rendering the initial state without potential flicker.
Sure. However, if I don't set stateB, it works as expected. Which makes it very confusing behavior. It only shows up if both are true:
Found the issue, thanks for the great repro @idubrov! I have a fix in the works, though may be delayed through reviews due to holidays..
@drarmstr I noticed that even after your fix was released I still had a case of an effect setting an initial value that was having its value get lost. It seemed related to this bug, so I'm mentioning it here. I opened PR #991 to demonstrate the incorrect behavior.
I spent a little time debugging the code to look for a fix based on your previous fix, but this seems different enough that a new fix didn't jump out at me. I hope my testcase can help somebody debug the problem. If you point me at a rough place that might be causing the problem, I'm happy to take another stab at fixing it myself
@mdlavin - Thank you very much for the test case! Yes, I suspect this test is unrelated to the original issue here.
Potentially, similar to #728.
Setting atom value inside an effect (via
setSelf
) while another update is happening loses the state change. DelayingsetSelf
viawindow.requestAnimationFrame
solves the issue.Here is the reproduction case I have: https://codesandbox.io/s/xenodochial-firefly-wny9s?file=/src/App.js
?delay
added to the URL.