polemius/recoil-persist

Support for asynchronous Storage (React Native)

BenJeau opened this issue · 2 comments

Using this package does not work with React Native since their Storage API (aka the AsyncStorage API, here's a link to their docs) is asynchronous and this package does not seem to support an asynchronous Storage API. This issue is to simply open the conversation this subject.

The following is what I used to set the storage:

const {persistAtom} = recoilPersist({
  storage: AsyncStorage,
});

The error message:

There's an error here since toParse is a Promise.
image

Looking at the code, you could check if getItem() or/and setItem() is a promise to know if its async or not, then transform the functions to be async. This is potentially a naïve idea, but I'm not sure how it could be implemented otherwise, maybe you have a better idea on its implementation.

@BenJeau Thank you for raising issue. I have updated the library and release and new version 2.1.0. Please try it now.

Thank you for the fast fix/update @polemius! I just pulled the latest version, there's no error and it seems to work well

Maybe it should be written in the README that it supports AsyncStorage too?