jotai-zustand: update to zustand store does not reflect in the jotai store #3084
-
Bug Descriptionimport { create } from 'zustand';
import { atomWithStore } from 'jotai-zustand';
import { getDefaultStore, useAtomValue } from 'jotai';
const zustandStore = create(() => 0);
const increment = () => zustandStore.setState((prev) => prev + 1);
const atom = atomWithStore(zustandStore);
const jotaiStore = getDefaultStore();
console.log(jotaiStore.get(atom)); // got 0, expect 0
increment();
console.log(jotaiStore.get(atom)); // got 0, expect 1 !! I was trying to access value out-of-react of an atom created by I could've got the value with Reproduction Linkhttps://stackblitz.com/edit/vitejs-vite-nini3fc9?file=src%2Fmain.js |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can manually subscribe to the atom, so it starts subscribing to the Zustand store: const unsub = jotaiStore.sub(atom, () => {}); |
Beta Was this translation helpful? Give feedback.
atomWithStore
usesonMount
to subscribe to the Zustand store and sync it with the Jotai atom.onMount()
will only be called when you subscribe to the atom but not if you just useget
+set
. If you use the atom inside React it will automatically work (as the hook subscribe to the atom).You can manually subscribe to the atom, so it starts subscribing to the Zustand store: