Persist not working on React Native
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 lose all the data.
import { createStore, applyMiddleware } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import ReduxThunk from 'redux-thunk';
import rootReducer from './reducers/';
import AsyncStorage from '@react-native-community/async-storage';
const persistConfig = {
key: 'root',
storage: AsyncStorage,
timeout: null
}
const persistedReducer = persistReducer(persistConfig, rootReducer)
const store = createStore(persistedReducer, {}, applyMiddleware(ReduxThunk))
const persistor = persistStore(store)
export { store, persistor };
Hi @mezalejandro,
Did you find a solution ?
Hello @FlorianMarcon It happens to me on android all the time, every time I open the app it does not load what it already had persisted.
i am sharing my config, in my case I am only persisting one reducer
const reducer = ...
const config = {
storage: AsyncStorage,
key: 'auth', // name of my reducer in the global store
whitelist: [ // note that i am only persisting this keys
'fullName',
'email',
],
};
export const persistedReducer = persistReducer(config, reducer);
import { persistedReducer as auth } from ...
export const reducer = combineReducers({
auth,
});
In my case i am using thunk.withExtraArgument to allow to use dependency injection pattern
const store = createStore(
reducer,
initialState,
applyMiddleware(
thunk.withExtraArgument(getDependencies())
)
);
const persistor = persistStore(
store,
undefined,
() => { console.log('done') }
);
UPDATE: I figured out my issue. My storage adapter was failing silently, causing the app to use the initialState next time around.
I know this issue is a bit old but while struggling with the same problem I noticed that a set is occurring immediately after hydration, as if the initialState is being saved over the persisted state. I haven't gotten to the bottom of it yet but maybe this will help someone narrow down the issue.
@ryanwillis I think I have the same issue, did you find the solution? I have reused some code from another app like 2 years ago and for some reason it's not working anymore
Hello @bengyles i try with redux-offline https://github.com/redux-offline/redux-offline Also use redux-persist. That way if it works for me
Oh no... after days of looking for a solution I finally noticed I put ['item, otheritem'] in whitelist instead of ['item', 'otheritem'] I feel so stupid now
I'm glad you could solve it!
@mezalejandro ,if the data in the store is above some limit, then redux persist will silently fail and won't persist that reducer data. Try to test your functionality with small piece of data and check whether it is a data size issue or not
import { combineReducers } from "redux"; import { persistReducer } from "redux-persist"; import storage from "redux-persist/lib/storage";
import cartReducer from "./cartSlice";
const persistConfig = { key: "root", storage, whitelist: ["cart"], };
const rootReducer = combineReducers({ cart: cartReducer, });
export default persistReducer(persistConfig, rootReducer);
this is the way how it works for me