Redux-persist: rehydrate fires after first dispatch

Created on 21 Jan 2018  路  2Comments  路  Source: rt2zz/redux-persist

If I put a manual dispatch just after createStore:

const store = createStore(
  persistedReducer,
  composeEnhancers(applyMiddleware(...))
);
const persistor = persistStore(store);
store.dispatch(myActionCreator());

the persist/REHYDRATE action fires after myAction. Is there a way to assure that REHYDRATE dispatches first?

Most helpful comment

you have a few options.

  1. move your dispatch into the persistStore callback:
const persistor = persistStore(store, null, () => {
  store.dispatch(myActionCreator())
})
  1. move your dispatch to be triggered somewhere inside of your component tree, inside of PersistGate
  2. use something like redux-action-buffer to capture the actions and delay their application

I would recommend them in that order, but of course it depends on your specific needs

All 2 comments

you have a few options.

  1. move your dispatch into the persistStore callback:
const persistor = persistStore(store, null, () => {
  store.dispatch(myActionCreator())
})
  1. move your dispatch to be triggered somewhere inside of your component tree, inside of PersistGate
  2. use something like redux-action-buffer to capture the actions and delay their application

I would recommend them in that order, but of course it depends on your specific needs

Option 1 worked great, big thanks!

Was this page helpful?
0 / 5 - 0 ratings