1 | import { atom } from 'jotai';
|
2 |
|
3 | function 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, update) => {
|
14 | const newState = typeof update === "function" ? update(get(baseAtom)) : update;
|
15 | store.setState(newState, true);
|
16 | });
|
17 | return derivedAtom;
|
18 | }
|
19 |
|
20 | export { atomWithStore };
|