Jotai v2.7.0+ Derived Atoms Not Recalculating Under Specific Conditions #2554
-
Hi dai-shi-san, In Jotai v2.7.0+ (including v2.8.0, latest), there appears to be a case where derived atoms are not being recalculated correctly. Sample Reproducible Code:Sample Code Overview and Expected Output:We create two primitive atoms ( const isFooAtom = atom(false);
const isBarAtom = atom(false);
const isActive1Atom = atom<boolean>((get) => {
return get(isFooAtom) && get(isBarAtom);
});
const isActive2Atom = atom<boolean>((get) => {
return get(isFooAtom) && get(isActive1Atom);
});
const activateAction = atom(undefined, async (get, set) => {
set(isFooAtom, true);
set(isBarAtom, true);
}); Since However, using the following code does not result in const App = () => {
const activate = useSetAtom(activateAction);
useAtomValue(isActive1Atom);
const isRunning = useAtomValue(isActive2Atom);
return (
<div>
<button onClick={() => activate()}>Activate</button>
{isRunning ? "running" : "not running"}
</div>
);
}; The issue occurs under the following conditions:
Workaround 1: Comment out the reading of isActive1Atom. // useAtomValue(isActive1Atom);
const isRunning = useAtomValue(isActive2Atom); Workaround 2: Rearrange the order of reading isActive1Atom. const isRunning = useAtomValue(isActive2Atom);
useAtomValue(isActive1Atom); This issue can be circumvented in simple cases like this example, but fixing it in a real application is quite challenging. It's not desirable, I think, for atom evaluation results to differ based on the presence or order of useAtomValue, and I would appreciate any insights or advice on this matter. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Thanks for reporting. It sounds like a regression with #2396. |
Beta Was this translation helpful? Give feedback.
#2555 should fix it.
https://ci.codesandbox.io/status/pmndrs/jotai/pr/2555
Please try it. See "Local Install Instructions" ☝️