Redux-persist: Purge() doesn't clear the store until app refresh?

Created on 17 Dec 2016  路  2Comments  路  Source: rt2zz/redux-persist

I have a stupid issue and I'm not sure if I'm doing something wrong, or there is a bug somewhere.

I have an app and I'm auto persisting the state and syncing with a server every X seconds. When I log out of the app - I would like to ideally - clear the store. This would signal my app that the user is not logged in and I show him the login screen.

I'm trying this

        purgeStoredState({storage: AsyncStorage}).then(() => {
            console.log('purge completed')
            Actions.LoginOrRegister();
        }).catch(() => {
            console.log('purge of someReducer failed')
        })

purgeStoredState seems to work fine - I land in my then function and I do my redirect to login screen. Unfortunately, the store is not cleared yet. If I try to login - I'm immediately logged in with my old data. Now if I close the app and then restart it - the state is cleared?

Do I need to do anything specific to clear the state?

Most helpful comment

purgeStoredState simply purges the state from storage, does not affect the state in redux itself. It sounds like what you want to do actually is reset redux to initial state. If you do this redux-persist will automatically store the reset state, and no purege will be necessary.

There are multiple ways to reset state in redux, the simplest of which is to make an action called RESET and implement it in your reducer(s). Another option if you do not want to have to toy with your reducers is to store initial state when the app loads, and then send a custom REHYDRATE action with the initial state. Something like:

const { REHYDRATE } . = require('redux-persist/constants')
//...
let initialState = reducer(undefined, { type: 'noop' })
//... sometime later
store.dispatch({
  type: REHYDRATE,
  payload: initialState
})

Hope that helps. There are a few options, really depends on the rest of your set up which is best!

All 2 comments

purgeStoredState simply purges the state from storage, does not affect the state in redux itself. It sounds like what you want to do actually is reset redux to initial state. If you do this redux-persist will automatically store the reset state, and no purege will be necessary.

There are multiple ways to reset state in redux, the simplest of which is to make an action called RESET and implement it in your reducer(s). Another option if you do not want to have to toy with your reducers is to store initial state when the app loads, and then send a custom REHYDRATE action with the initial state. Something like:

const { REHYDRATE } . = require('redux-persist/constants')
//...
let initialState = reducer(undefined, { type: 'noop' })
//... sometime later
store.dispatch({
  type: REHYDRATE,
  payload: initialState
})

Hope that helps. There are a few options, really depends on the rest of your set up which is best!

@rt2zz thanks Zack! You saved me a lot of head scratching :)

I implemented a root reducer as suggested here http://stackoverflow.com/a/35641992 and that works great.

Was this page helpful?
0 / 5 - 0 ratings