atomWithStorage - How is localStorage v.s. AsyncStorage decided? #1146
-
I'm working on a project in which our state is using jotai and is stored in a common project that is used in both web and mobile. One of our state atoms defined in this common project uses the
If I simply use I am wondering - How does jotai know when to use AsyncStorage and when to use local storage? AsyncStorage is a runtime dependency of the mobile project and is working when used directly outside of jotai. How can I tell jotai to use AsyncStorage when running on mobile, but use localStorage on the web, without directly referencing either? Is this even possible? If not, it may be nice to provide a way for setting the storage provider globally for jotai, rather than on a per Maybe I missed it but I didn't really see anything about this in the docs: https://jotai.org/docs/utils/atom-with-storage |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Sorry, the doc is missing for custom storage usage. It would be so nice if someone can contribute on it.
It sounds like you know how to use is for per const atomWithStorageAuto = (key, initialValue) => {
let storage;
if (it has async storage) {
storage = createJSONStorage(() => AsyncStorage);
} else {
storage = createJSONStorage(() => localStorage);
}
return atomWithStorage(key, initialValue, storage);
}; |
Beta Was this translation helpful? Give feedback.
Sorry, the doc is missing for custom storage usage. It would be so nice if someone can contribute on it.
It sounds like you know how to use is for per
atomWithStorage
basis?In that case, my suggestion is to define a new function on your end.