Skip to content

atomWithStorage - How is localStorage v.s. AsyncStorage decided? #1146

Closed Answered by dai-shi
johnrhombus asked this question in Q&A
Discussion options

You must be logged in to vote

Sorry, the doc is missing for custom storage usage. It would be so nice if someone can contribute on it.

If not, it may be nice to provide a way for setting the storage provider globally for jotai, rather than on a per atomWithStorage basis...?

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.

const atomWithStorageAuto = (key, initialValue) => {
  let storage;
  if (it has async storage) {
    storage = createJSONStorage(() => AsyncStorage);
  } else {
    storage = createJSONStorage(() => localStorage);
  }
  return atomWithStorage(key, initialValue, storage);
};

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@johnrhombus
Comment options

@dai-shi
Comment options

@johnrhombus
Comment options

@TwistedMinda
Comment options

Answer selected by johnrhombus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1145 on May 06, 2022 23:24.