I have created this custom atom that uses the idb-keyval library to get and set values from indexeddb:
export function atomWithAsyncStorage<T>(key: string, initial?: T) {
const store = createStore("conexsys-db", "keyval-store");
return atomWithStorage<T>(key, initial, {
setItem: (key, newValue) => set(key, newValue, store),
getItem: (key) =>
get<T>(key, store).then((value) => {
if (value !== undefined) {
return value;
}
if (initial !== undefined) {
set(key, initial, store);
}
return initial;
}),
removeItem: (key) => del(key),
});
}
This atom can then be created this way:
export const contextsAtom = atomWithAsyncStorage("CONTEXTS", []);
const [contexts, setContexts] = useAtom(contextsAtom);
The thing about the above atom is it uses the "CONTEXTS" value in the indexedDB store, but how can I dynamically from my react component set any value to indexedDB using Jotai too?
For example, if I have a URL id, how can I do something like:
const [contexts, setContexts] = useAtom(contextsAtom, "usethisid"); OR
const [contexts, setContexts] = useAtom(contextsAtom("usethisid"));