UNPKG

459 BJavaScriptView Raw
1import { atom } from 'jotai';
2
3function atomWithStore(store) {
4 const baseAtom = atom(store.getState());
5 baseAtom.onMount = (setValue) => {
6 const callback = () => {
7 setValue(store.getState());
8 };
9 const unsub = store.subscribe(callback);
10 callback();
11 return unsub;
12 };
13 const derivedAtom = atom((get) => get(baseAtom), (_get, _set, action) => {
14 store.dispatch(action);
15 });
16 return derivedAtom;
17}
18
19export { atomWithStore };