-
Notifications
You must be signed in to change notification settings - Fork 39
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
Comments
same here |
Same here, any solution? |
You don't need to use this library to get simple persistence with Recoil and React Native. Just adapt the 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 <React.Suspense fallback={<View><Text>Loading</Text></View>}>
<RecoilRoot>
{/* your app components */}
</RecoilRoot>
</React.Suspense> |
I had to adapt the code from @fagerbua cause I was getting blank screens. This worked better for me, copying the latest recoil documentation:
|
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"
The text was updated successfully, but these errors were encountered: