question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Persist not working with AsyncStorage (React Native)

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

8reactions
macrozonecommented, Feb 24, 2020

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

0reactions
robertvorthmancommented, May 24, 2020

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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Persist not working on React Native · Issue #1253 - GitHub
Hello, I'm using react native version 0.62.0 and redux-persist version 5.10.0. It is not working for me when I enter the app I...
Read more >
redux persist not storing store in the async storage
My redux-persist version is 6.0.0. Now I come to my problem. My react-native app is fetching data from json-server and storing it in...
Read more >
How To Use Redux Persist in React Native with Asyncstorage
The Redux Persist library provides an easy way to save a Redux store in the local storage of React Native apps. In this...
Read more >
React Native Async Storage (persist) Patterns for beginners
Persisting Values in App is a cool not-so-secret. The use cases of AsyncStorage are pretty simple to use. You can set values to...
Read more >
redux-persist-reborn - npm
Web: no breaking changes React Native: Users must now explicitly pass their storage engine in. e.g.. import AsyncStorage from '@react-native- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found