Some atoms do not re-render if other atoms are conditionally read them #2697
Replies: 2 comments 14 replies
-
The sandbox isn't public. |
Beta Was this translation helpful? Give feedback.
-
So, it's clearly a regression in store2 introduced in v2.9.0.
I was hoping to keep the logic of
I don't look it very closely, but it sounds fair.
I can't judge that either. Can you open a PR and see if how it goes? I would like you to first add a failing test and open a PR (which runs CI and fails), |
Beta Was this translation helpful? Give feedback.
-
Bug Description
Hi,
I found atoms some times not updated dependancies if I use early return or other conditional statements in derived atom.
I have created the following atoms: 1 primitive atom and 3 derived atoms. The last one conditionally depends on the other 2 derived atoms. I think it's not a particularly special use case.
At the initial render, the values of all 3 derived atoms are
undefined
.Then I set a value to the primitive atom.
I expected
nameAtom
andageAtom
to be both recalculated. However, the value ofuseAtomValue(ageAtom)
remainsundefined
, not12
.This problem occurs under:
It does not occur under:
I don't know much about the core store code but traced what happened in
recomputeDependents
. This might be of some help.topsortedAtoms
:topsortedAtoms
from the last one.In this process,
ageIfNameAtom
is recomputed beforeageAtom
, andageAtom
is updated to12
but not added topending
at this time. In the next loop oftopsortedAtoms
,ageAtom
is read again, but because theepochNumber
has already been incremented byageIfNameAtom
,ageAtom
is not added topending
. Eventually, the value of theageAtom
atom state is updated but does not cause a re-render ofuseAtomValue(ageAtom)
.If I comment out the early return from
ageIfNameAtom
,the order of
topsortedAtoms
is changed, andageAtom
is evaluated beforeageIfNameAtom
.However, I think that we can't detect changes in
ageIfNameAtom
's dependencies before reading it, making it impossible to listageIfNameAtom
beforeageAtom
intopsortedAtoms
.I think we might need to check for changes in epoch numbers not only in
topsortedAtoms[i]
itself but also in its dependencies. The following patch (looks) works in my case:But I can't determine its overall effect and can't judge this approach is right or not.
Reproduction Link
https://codesandbox.io/p/sandbox/jotai-2-9-d7yfkn
Beta Was this translation helpful? Give feedback.
All reactions