Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncStorage implementation issue. #45

Open
dezudas opened this issue Aug 23, 2021 · 5 comments
Open

AsyncStorage implementation issue. #45

dezudas opened this issue Aug 23, 2021 · 5 comments

Comments

@dezudas
Copy link

dezudas commented Aug 23, 2021

will it be possible to provide an example for AsyncStorage when I tried it says "Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'hasOwnProperty' of null"

@deargosep
Copy link

same here

@fervailanti
Copy link

Same here, any solution?

@fagerbua
Copy link

fagerbua commented Dec 31, 2021

You don't need to use this library to get simple persistence with Recoil and React Native. Just adapt the localForage example from the Recoil documentation, and you get (with TypeScript):

import {atom, AtomEffect, DefaultValue} from "recoil";

function persistAtom<T>(key: string): AtomEffect<T> {
  return ({setSelf, onSet}) => {
    setSelf(AsyncStorage.getItem(key).then(savedValue =>
      savedValue != null
        ? JSON.parse(savedValue)
        : new DefaultValue() // Abort initialization if no value was stored
    ));

    // Subscribe to state changes and persist them to localForage
    onSet((newValue, _, isReset) => {
      isReset
        ? AsyncStorage.removeItem(key)
        : AsyncStorage.setItem(key, JSON.stringify(newValue));
    });
  };
}

const myAtom =  atom({key: 'my-key', default: {}, effects_UNSTABLE: [persistAtom('my-async-storage-key')]})

Then wrap your RecoilRoot with a Suspense component:

      <React.Suspense fallback={<View><Text>Loading</Text></View>}>
        <RecoilRoot>
          {/* your app components */}
        </RecoilRoot>
      </React.Suspense>

@cuzzlor
Copy link
Contributor

cuzzlor commented Feb 14, 2023

Despite the typings, it doesn't seem to support async getItem or setItem.

image

@ipoogleduck
Copy link

I had to adapt the code from @fagerbua cause I was getting blank screens. This worked better for me, copying the latest recoil documentation:

function persistAtom<T>(key: string): AtomEffect<T> {
    return ({ setSelf, onSet, trigger }) => {
        const loadPersisted = async () => {
            const savedValue = await AsyncStorage.getItem(key);

            if (savedValue != null) {
                setSelf(JSON.parse(savedValue));
            }
        };

        // Asynchronously set the persisted data
        if (trigger === 'get') {
            loadPersisted();
        }

        // Subscribe to state changes and persist them to localForage
        onSet((newValue, _, isReset) => {
            isReset
                ? AsyncStorage.removeItem(key)
                : AsyncStorage.setItem(key, JSON.stringify(newValue));
        })
    }
}

const myAtom =  atom({key: 'my-key', default: {}, effects_UNSTABLE: [persistAtom('my-async-storage-key')]})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants