rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.95k stars 865 forks source link

Difference between flush and purge? Do I need to flush after resetting store and then purge? #599

Open claykohut opened 6 years ago

claykohut commented 6 years ago

I'm trying to clear my app's state and persisted data -- and then do some various action.

Before, I was just doing something like:

persistor.purge()
    .then(() => {
           this.props.resetStore();
           // do something here like 
           // add new data to store
     })

But it seems like sometimes if i close the app and reopen quick enough the app will have cleared the store but not saved the new state.

Now i'm doing something like:

  this.props.resetStore();
  persistor.flush()
     .then(() => {
         persistor.purge()
            .then(() => {
                /// add new data to store here
                  persistor.flush()
                    .then((result) => {
                          /// now go to new route
                     })
              })
       })

This seems like super overkill, so maybe I'm not understanding the point of flush -- does flush force a save on the pending state data? should flush be called before a purge?

If I'm resetting the state of the store and want to make sure the cleared store sticks, do I need to resetStore, then flush, then purge?

Sorry if this is a bit confusing.

Thanks

rt2zz commented 6 years ago

if all you want is to reset state, you should be able to simple dispatch your resetStore action, then redux-persist will store the reset state as soon as it can.

purge is meant for clearing state explicitly, usually done as part of development when state gets into a bad place.

flush is designed to force the writing of all pending state asap, and to provide a promise to wait for the writes resolution. This can be handy for example if you need to ensure latest state is written before navigating away.

ChrisLahaye commented 6 years ago

@rt2zz I encounter the same problem. After performing my redux-reset action, my actual redux store reflects the right information. However, after explicitly flushing the persistor and restarting the app, the redux store has the information from before the reset.