[2.11.1+] Derived atoms are not re-evaluated after referencing them immediately after updating an atom that changes dependencies. #2964
Replies: 1 comment 1 reply
-
Thank you for reporting. Note to self, just thinking about this on my way in to work: when writeAtomState on activateAction is called, setter is called on isActiveAtom which updates the value to true. isActiveAtom is marked as dirty along with all its dependents; since isActiveAtom is currently mounted, this includes activeCountAtom. Calling getter inside writeAtomState should cause an eager readAtomState on activeCountAtom. Since activeCountAtom is dirty, this should recursively reevaluate all dependencies. In this case, countAtom is conditional and is not yet a dependency of activeCountAtom. So the issue lies with not
|
Beta Was this translation helpful? Give feedback.
-
Bug Description
Starting from jotai v2.11.1, there is an issue where derived atoms do not re-evaluate under certain conditions.
The following code reproduces this problem.
This code implements a simple counter. The displayed count is provided by a derived atom that only return value when
isActiveAtom
is true.Running this code and executing
activateAction
and updatingcountAtom
,although the counter is expected to increment,the counter value does not change from 0.
Suspected Issue:
The problem seems to be caused by referencing a derived atom immediately after updating a dependent atom, which prevents further re-evaluations. Removing the seemingly innocuous
get(activeCountAtom)
insideactivateAction
makes the code function as expected.When
set(isActiveAtom, true)
changes the dependencies ofactiveCountAtom
, callingget(activeCountAtom)
before the dependency recalculation caused by this change leads to the issue.Reproduction Link
https://codesandbox.io/p/sandbox/fmk5xf
Beta Was this translation helpful? Give feedback.
All reactions