-
Hi, An atom with a getter const foo = atom(() => Date.now());
<ComponentThatUsesFoo /> { /* Calls the atom getter */ }
<ComponentThatUsesFoo /> { /* Reuses the cached value /* }
<ScopeProvider atoms={[]}>
<ComponentThatUsesFoo /> { /* Calls the atom getter again */ }
</ScopeProvider> Idk, maybe this is needed because the atom could depend on a scoped atom... Is this a bug or the expected behavior? We recently added Jotai Scope to our app and this resulted in a bug with existing atoms (that are not scoped). Our getter is used to create the initial value which is then expected to be shared across all usages of the atom. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'd say it's a designed behavior. I'm not 100% sure if I understand your problem with jotai-scope, but I think the following is a workaround. const now = Date.now();
const foo = atom(() => now); |
Beta Was this translation helpful? Give feedback.
I'd say it's a designed behavior.
Though, it's unfortunate our API doesn't cover some cases like lazy initialization for primitive atoms and eager initialization for read-only atoms. It's a design limitation. (We might add some new api to cover those, but they are mostly rare cases.)
I'm not 100% sure if I understand your problem with jotai-scope, but I think the following is a workaround.