easy-peasy
easy-peasy copied to clipboard
Persist doesnt work with react-native-async-storage/async-storage
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

but after upgrading it looks like

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
Same here, tried with both v5 and v4, setItem is never being called on storage
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
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.
Is there any progress on this issue ?
FWIW, we've been using this same setup in [email protected] without problem. And just upgraded to v6 with no problem.
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)?
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.
Thanks for checking @damassi! At least there's an acceptable workaround.
Coming back to this, I realized we were already using the
requestIdleCallback = nullhack, 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.