easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

Persist doesnt work with react-native-async-storage/async-storage

Open OsapBender opened this issue 4 years ago • 4 comments

Today I upgraded easy-peasy from v3.3.1 to v5.0.3 and my async store doesnt work anymore. I have persistor

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

import {persist} from 'easy-peasy';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export default (reducer) =>
  persist(reducer, {
    storage: {
      async getItem(key) {
        return JSON.parse(await AsyncStorage.getItem(key));
      },
      setItem(key, data) {
        const jsonValue = JSON.stringify(data);
        console.log('key2: ', key, jsonValue);
        AsyncStorage.setItem(key, jsonValue);
      },
      removeItem(key) {
        console.log('key3: ', key);
        AsyncStorage.removeItem(key);
      },
    },
  });

on v3.3.1 my console log was like Снимок экрана 2021-08-29 в 17 04 51

but after upgrading it looks like Снимок экрана 2021-08-29 в 17 06 44

my store model:

import {Action, action} from 'easy-peasy';
import persistor from '../persistor';
import {keys, storeAsyncData} from '../asyncStorage';

export interface IAuthModel {
  isAuth: boolean;
  setAuth: Action<IAuthModel, boolean>;
}

const authModel: IAuthModel = {
  isAuth: false,
  setAuth: action((state, payload: boolean): void => {
    state.isAuth = payload;
  }),
};

const thunks = {};

export default persistor({
  ...authModel,
  ...thunks,
});

import AppModel, {IAppState} from './app';
import Auth from './auth';
import {IAuthModel} from './auth';

export interface IStoreModel {
  app: IAppState;
  auth: IAuthModel;
}

const storeModel: IStoreModel = {
  app: AppModel,
  auth: Auth,
};

export default storeModel;

if i downgrade easy-peasy from 5.0.3 to 3.3.1 it works fine.

What's wrong?

PS I read that changes from 3.3.1 to 5.0.3 but cannot find a solution

OsapBender avatar Aug 29 '21 13:08 OsapBender

Same here, tried with both v5 and v4, setItem is never being called on storage

tamagokun avatar Oct 19 '21 09:10 tamagokun

Confirmed I had this issue when trying to upgrade to v5, I'm stuck at v3 for this reason. If I have the luxury of bonus time over the holidays maybe I can take a look and see what's going on

rpadilla6 avatar Dec 09 '21 17:12 rpadilla6

This may be the issue for anyone reading this -- certainly was for me after the v3 -> v5 upgrade. There is a workaround in this thread.

The root cause is this RN regression.

rbruels avatar Jan 28 '22 06:01 rbruels

Is there any progress on this issue ?

sefaokumus avatar Sep 08 '22 14:09 sefaokumus

FWIW, we've been using this same setup in [email protected] without problem. And just upgraded to v6 with no problem.

damassi avatar Apr 21 '23 17:04 damassi

Thanks @damassi! Is it possible to post a simple example of the setup that's working for you in case anyone else is blocked by this? As suggested by @rbruels the root cause may have been an RN regression issue, so I'm wondering if maybe this is fixed in newer versions (though it doesn't appear to have been addressed on the linked issue)?

no-stack-dub-sack avatar May 05 '23 15:05 no-stack-dub-sack

Coming back to this, I realized we were already using the requestIdleCallback = null hack, and that is why things were working! so working around the regression.

damassi avatar May 05 '23 16:05 damassi

Thanks for checking @damassi! At least there's an acceptable workaround.

no-stack-dub-sack avatar May 06 '23 01:05 no-stack-dub-sack

Coming back to this, I realized we were already using the requestIdleCallback = null hack, and that is why things were working! so working around the regression.

This is now fixed in v6.0.1, which means that the requestIdleCallback = null workaround is no longer required for easy-peasy.

jmyrland avatar Jun 14 '23 15:06 jmyrland