rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.96k stars 866 forks source link

[v5] Store no longer persists after purge #435

Closed leethree closed 7 years ago

leethree commented 7 years ago

Steps:

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.

rt2zz commented 7 years ago

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

leethree commented 7 years ago

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!

leethree commented 7 years ago

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.

laksh1010 commented 6 years ago

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

leethree commented 6 years ago

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

skoob13 commented 6 years ago

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