Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge() doesn't clear the store until app refresh? #238

Closed
compojoom opened this issue Dec 17, 2016 · 2 comments
Closed

Purge() doesn't clear the store until app refresh? #238

compojoom opened this issue Dec 17, 2016 · 2 comments

Comments

@compojoom
Copy link

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?

@compojoom compojoom changed the title Purge() doesn't clean the store until app refresh? Purge() doesn't clear the store until app refresh? Dec 17, 2016
@rt2zz
Copy link
Owner

rt2zz commented Dec 18, 2016

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!

@compojoom
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants