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

Persist not working with AsyncStorage (React Native) #431

Closed
24dev opened this issue Feb 16, 2020 · 5 comments
Closed

Persist not working with AsyncStorage (React Native) #431

24dev opened this issue Feb 16, 2020 · 5 comments

Comments

@24dev
Copy link

24dev commented Feb 16, 2020

easy-peasy: v3.2.0 & v3.3.0
expo: v~36.0.0

When using AsyncStorage, the entire expo app crashes.

IMG_0339

Only when passing AsyncStorage to the persist config, like so. Putting 'localStorage' does not error.

import { AsyncStorage } from 'react-native';
import { createStore, persist } from 'easy-peasy';

export default createStore(
  persist(store, {
    storage: AsyncStorage,
  }),
);

Maybe down to not stringifying the values: https://stackoverflow.com/questions/49491485/error-react-native-ios-exception-nsdictionarym-length-unrecognised-select

@systemlevel
Copy link

None of the persist methods for react native have worked for us either.

@24dev
Copy link
Author

24dev commented Feb 20, 2020

@systemlevel How are you planning on implementing offline support instead?

@macrozone
Copy link

@ctrlplusb do you have any clue on this?

@macrozone
Copy link

solution is to slightly tweak the storage to serialize:


import AsyncStorage from '@react-native-community/async-storage'

const storage = {
  async getItem(key) {
    return JSON.parse(await AsyncStorage.getItem(key))
  },
  setItem(key, data) {
    AsyncStorage.setItem(key, JSON.stringify(data))
  },
  removeItem(key) {
    AsyncStorage.removeItem(key)
  }
}

export default storage

@robertvorthman
Copy link

solution is to slightly tweak the storage to serialize:


import AsyncStorage from '@react-native-community/async-storage'

const storage = {
  async getItem(key) {
    return JSON.parse(await AsyncStorage.getItem(key))
  },
  setItem(key, data) {
    AsyncStorage.setItem(key, JSON.stringify(data))
  },
  removeItem(key) {
    AsyncStorage.removeItem(key)
  }
}

export default storage

Thanks! I had to add async in front of setItem and removeItem or else I would get an error "Cannot read property 'catch' of undefined" from the writeStatedState function of createPersistoid.js in redux-persist.

This is what worked for me:

import AsyncStorage from '@react-native-community/async-storage'

const storage = {
  async getItem(key) {
    return JSON.parse(await AsyncStorage.getItem(key))
  },
  async setItem(key, data) {
    AsyncStorage.setItem(key, JSON.stringify(data))
  },
  async removeItem(key) {
    AsyncStorage.removeItem(key)
  }
}

export default storage

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

4 participants