Redux-persist: [v5] Store no longer persists after purge

Created on 1 Sep 2017  路  6Comments  路  Source: rt2zz/redux-persist

Steps:

  • store something and persists
  • persistor.purge()
  • modify reducers.

Expected:
New changes are persisted. Restart the app the store should rehydrate normally.

Actual:
Store no longer persists to storage. Restart the app and the stored data is gone.

Most helpful comment

I solved the issue by calling persistStore() again after the entire store is cleared (_persist is missing). Here's my code:

async () => {
  // this action will clear everything in the store
  store.dispatch(storeReset());
  // purge persisted data
  persistor.purge();
  // re-persist store
  await new Promise(resolve => persistStore(store, {}, resolve));
};

But the purge function is no longer async, which might cause issue in persistStore because the purge might still in progress. I'll open another issue for that.

All 6 comments

I just tested it and it works as expected (new changes after the purge are persisted). Are you on web or native?

I'm using it on native. I think I found the problem. I'm using persistReducer on sub reducers, and I need to clear the entire store when the user logged out.

When the root reducer is cleared, the _persist keys in the sub reducers are cleared too. And the persistReducer function will refuse to persist store when _persist is missing. (persistReducer.js line 121)

Should the reducer reconstruct the _persist key when it's reset? Or is there any suggested way to handle this use case? Thanks!

I solved the issue by calling persistStore() again after the entire store is cleared (_persist is missing). Here's my code:

async () => {
  // this action will clear everything in the store
  store.dispatch(storeReset());
  // purge persisted data
  persistor.purge();
  // re-persist store
  await new Promise(resolve => persistStore(store, {}, resolve));
};

But the purge function is no longer async, which might cause issue in persistStore because the purge might still in progress. I'll open another issue for that.

hi can i know how u defined storeReset at store.dispatch(storeReset());

@laksh1010 you can use something like redux-reset or implement your own.

One more solution is to call persistor.persist() after purging.

Was this page helpful?
0 / 5 - 0 ratings