Replies: 6 comments 5 replies
-
Part of the issue is that the atomEffect's EffectFn runs on next tick. So in the current tick, atoms whose only mount point is from inside the atomEffect will unmount on current tick and remount on next tick when the EffectFn runs again. This seems to only affect Writeable Atoms that have a mount function where the One possible solution is to store a set of all atoms "seen" in the synchronous execution of the EffectFn. Then, to apply There are a few issues with doing this. First, its a perf hit to unnecessarily fetch atom values only to ensure they stay mounted. Second, its a perf hit, because we are keeping references of atoms outside the weakMap. Third, bistable atom dependencies are not properly handled. atomEffect((get, set) => {
if (get(boolAtom)) {
get(atomA)
} else {
get(atomB)
}
}) When boolAtom is true, the atom dependencies are [boolAtom, atomA]. When bool atom is false, the atom dependencies are [boolAtom, atomB]. If we assume the previous runs atom dependencies are valid in the current run, then the following happens:
|
Beta Was this translation helpful? Give feedback.
-
Related PR: #23 |
Beta Was this translation helpful? Give feedback.
-
Actually, it doesn't. I just added |
Beta Was this translation helpful? Give feedback.
-
Outside React, |
Beta Was this translation helpful? Give feedback.
-
I found a workaround for preventing infinite loops with nested atomEffects. Its a bit counter-intuitive, but instead of calling setSelf on mount, its called on next tick from the initAtom's read fn. Although the remounting bug still exists for other atoms that have an onMount property. const initAtom = atom(
(get, { setSelf }) => {
const ref = get(refAtom)
if (ref.mounted) return
Promise.resolve().then(() => {
setSelf(true)
})
},
(get, set, mounted: boolean) => {
const ref = get(refAtom)
ref.mounted = mounted
if (mounted) {
set(refreshAtom, (c) => c + 1)
} else {
ref.cleanup?.()
ref.cleanup = undefined
}
}
)
initAtom.onMount = (init) => () => init(false)``` |
Beta Was this translation helpful? Give feedback.
-
This issue has been fixed by jotai-core! 🥳 ❌ Failing example with [email protected], [email protected]: https://codesandbox.io/p/sandbox/pedantic-forest-sckny6 ✅ Fixed example with [email protected], [email protected]: https://codesandbox.io/p/sandbox/bold-andras-crwwmv |
Beta Was this translation helpful? Give feedback.
-
Related Issue: #21
Atoms that are mounted only inside atomEffects may unmount between runs.
This will happen if:
This edge case may cause infinite loops in nested atomEffect dependencies.
Consider the following example:
https://codesandbox.io/p/sandbox/pedantic-forest-sckny6?file=%2Fsrc%2FApp.tsx%3A41%2C12
Beta Was this translation helpful? Give feedback.
All reactions