redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Persist not working on React Native

Open mezalejandro opened this issue 5 years ago • 10 comments

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 };

mezalejandro avatar Oct 08 '20 03:10 mezalejandro

Hi @mezalejandro,

Did you find a solution ?

FlorianMarcon avatar Nov 04 '20 15:11 FlorianMarcon

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.

mezalejandro avatar Nov 11 '20 10:11 mezalejandro

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') }
  );

arcollector avatar Nov 11 '20 16:11 arcollector

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 avatar Jan 05 '21 10:01 ryanwillis

@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

bengyles avatar Jan 18 '21 18:01 bengyles

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

mezalejandro avatar Jan 18 '21 18:01 mezalejandro

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

bengyles avatar Jan 18 '21 18:01 bengyles

I'm glad you could solve it!

mezalejandro avatar Jan 18 '21 19:01 mezalejandro

@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

deerajDev avatar Jan 27 '23 10:01 deerajDev

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

mahmoud-mohasseb avatar Mar 02 '23 16:03 mahmoud-mohasseb