redux-persist: redux-persist is not working

I followed doc here for setup --> https://github.com/rt2zz/redux-persist

Basically I’ve simple basic setup as per the document

import { createStore } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import user from './reducers/user';

const persistConfig = {
  key: 'root',
  storage,
}

const rootReducer = combineReducers({user: user});

const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = createStore(persistedReducer, applyMiddleware(thunk));

const persistor = persistStore(store);

export { store, persistor } ;

And then, I add PersistGate in my render.

ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor} >
      <RootRouter />
    </PersistGate>
  </Provider> ,
  document.getElementById('root')
);

But still when I refresh my page, it doesn’t keep those states active. The only difference I can see in my code (than document) is that I’m using thunk. Can someone please help me?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 13
  • Comments: 34

Most upvoted comments

Hi guys, I found a solution to this problem. (might not apply to all the cases, but worked for me). The problem was caused by my default return in my reducer, which I returned {...state}. Once I changed the default return to just return state, everything work as expected.

sometimes it calls error with key in persistConfig. try key: ‘primary’

Was banging my head against this for longer than I want to say… thanks @hackrit ! that fixed it for me