utils - Atom not found
See original GitHub issueI have a utility component where I create some atoms, and maybe set some values depending on props and other stuff, but I don’t need the values, I export the value getters separately.
So I am using useUpdateAtom in this component instead of useAtom, as I assume it would have some performance benefits.
The problem is always get this error:
[Bug] Atom state not found. Please file an issue with repro: updateAtomState
{key: 1, init: 1, read: ƒ (), write: ƒ ()}
I tracked this down and it appears there is some setup work to initialize the atom’s state that is done in useAtom, but when using useUpdateAtom, this init is only done on the read only copy of the atom. https://github.com/pmndrs/jotai/blob/072e26a41f590adf6b7e8e8683d48cc3d30caddb/src/utils/useUpdateAtom.ts#L11
In that line, useAtom (and the init contained in it) is run for writeOnlyAtom
, but not for anAtom
.
The init I think is missing is here:
https://github.com/pmndrs/jotai/blob/072e26a41f590adf6b7e8e8683d48cc3d30caddb/src/core/useAtom.ts#L66
I have a minimal repro here: https://codesandbox.io/s/jotai-atom-state-not-found-9dm2t just push the button and look at the console =)
BTW, thanks for the library, its great!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
ah, I see… I thought that might be the case… I will make sure to initialize them before using them. Thank you!
Good morning.
Exactly. It would behave the same with React.useState.
You could change the behavior with a derived atom.
Would you create a small repro in codesandbox? Oh, I think I get it. You are probably trying to update an atom that is never used. Currently, an atom must be used explicitly or dependent with other atoms. You can’t update it before the initialization. We discuss about this behavior recently, and hope to make a better documentation, give better warning message or fix it fundamentally if possible.