Redux-persist: Re: Purge. State persists until after hard refresh or app restart

Created on 2 Jul 2017  路  14Comments  路  Source: rt2zz/redux-persist

Hi all,

This may be a feature, not a bug. I have an app with a subscription model. When the user unsubscribes, I want to remove all the premium content. For this I'm purging the store. However, the state still persists until after I cmd + r or close and restart the app.

I want the purge to be treated as if I'm updating props. Is there another way I can make the components recognize that props have changed without having to restart the app or cmd + r?

Thanks

All 14 comments

I also have the same problem. I have to clear my cache in order for new state to be reflected on the state tree.

EDIT: Will try it redux-persist-migrate

@arvinsim I would recommend trying redux-persist@5 npm i redux-persist@next which has migrations built in. See the readme for basic docs: https://github.com/rt2zz/redux-persist/tree/v5

@rt2zz

So v5 is already production ready? Only reason I picked v4 is that there is lots of support for it.

Myself and at least a handful of others are using v5 in production. It works great for the standard usage, I would only caution that there may be potential issues at the edges. For example there is not yet top level immutablejs support.

@rt2zz Thanks for confirming that. I am not using immutablejs so I guess I could give it a go ahead.

I think I have the same problem that @guymorita, when I call to persistor.purge() the data on localStorage is being removed but the props still have the old values until I refresh the web page. I have searched the full code and didn't find anything that lead me to thing removal is being notified to redux core so it can be able to clean up its internal structures, and that's the reason why props are still being filled...

If it's of interes, I'm using redux-persist under redux-offline under react-native-web.

@piranna purge only purges stored state, leaves the redux state unaffected. If you want to reset redux state that is outside of the scope of this module. There are multiple ways to go about this, but in general I find most redux applications implement a RESET action.

I find most redux applications implement a RESET action

Isn't there a native action for that, the same way redux-persist has persist/PURGE? I find it a somewhat basic functionality, and so I though redux-persist was already calling it... It's said, do I need to reset myself the redux state?

@piranna not that I know of. In order for redux-persist to do something like that it would need to make some assumptions about initial state that I do not think would be fair. That said it could probably be done with something like:

let initialState = reducer(undefined, { type: 'INITIAL_STATE_PROBE' })
store.dispatch({
  type: REHYDRATE,
  key: yourPersistKey,
  payload: initialState,
})

This could probably be pulled out into its own module, something like

import reduxPersistReset from 'redux-persist-reset'

reduxPersistReset(store, persistConfig)

I though that since it's being defined the initialState of each reducer redux was already conscious of it...

Your code seems interesting, if I understood it, it's executing the reducer without state to fetch the initial state and storing it elsewhere, and later using REHYDRATE to force setting the value of the internal redux state, isn't it? When you said that I would need to reset it myself I was thinking about adding a new persists/PURGE case on the diferent reducers and set the returned value to the default one, although I would need then to define it outside the default attribute. Definitely your approach is better than mine :-) Would you like to give it a go or do you preffer I try it first?

go for it, I think it could be useful functionality for sure its a common use case!

Well, I'vo fpund that the redux-reset package already does the job, and in fsct it does it the way you proposed! :-P

hah fantastic! We will have to add that as a footnote to the purge docs.

hah fantastic! We will have to add that as a footnote to the purge docs.

Done :-)

Was this page helpful?
0 / 5 - 0 ratings