UNPKG

475 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(
14 (get) => get(baseAtom),
15 (_get, _set, action) => {
16 store.dispatch(action);
17 }
18 );
19 return derivedAtom;
20}
21
22export { atomWithStore };