Atomをincrementして変更をトリガーするハック
更新時に、counterをincrementして参照用atomに更新を伝える
Best Practices for Synchronizing Atoms with Remote Storage · pmndrs jotai · Discussion #2583 · GitHub
code:ts
const _eventsAtom = unwrap(
atom(async get => {
get(updatedCounterAtom);
const id = get(idAtom);
return await getEventsById(id);
}),
e => e ?? [],
);
export const saveAtom = atom(
null,
async (get, set, event: Event) => {
const id = get(idAtom);
await addEventById(id, event);
set(updatedCounterAtom, c => c + 1);
},
);
const updatedCounterAtom = atom(0);