Giter Site home page Giter Site logo

Comments (8)

rt2zz avatar rt2zz commented on August 15, 2024 2

You can do this a few different ways. Basically the crux is you need to get a handle on the persistor object that is returned by persistStore. If this UI button lives deep inside your app, you will either need to

  1. expose persistor via context similar to how react-redux Provider works
  2. pass the persistor down via props
  3. create persistor as a singleton which can be required in your other components.

Can you explain more about your use case? purge clears the local storage but does not reset state. I suspect what you actually are looking for is an action that resets state to initialState. To do this I would not worry about the persistor at all, and instead create your own action called "RESET_STATE" which each reducer is responsible for implementing.

Let me know if any of those work for you.

from redux-persist.

ericlau-solid avatar ericlau-solid commented on August 15, 2024 2

Thanks for the quick reply - there are 2 use cases:

  • one is for production. Say I have version 1.2 on PROD, and I want to deploy 1.3. Version 1.3 has some state-shape changes which make it incompatible with 1.2. How do I go about clearing out local storage redux state data that is incompatible, and ensure a smooth experience when user hits the site (at 1.3), with a local storage state which is fine in 1.2, but not 1.3.
  • one is for development -- we often find ourselves needing a quick button to clear out local storage. We have a workaround where we simply clear the localStorage itself (not using purge), and trigger a location.reload(). --- this is also useful in case there's a bug with the last use case and end-user needs a way to clear all the local storage and refresh.

from redux-persist.

rt2zz avatar rt2zz commented on August 15, 2024

Got it. I do not have a good solution to number 1. Generally I write custom code to handle this kind of situation on a per reducer basis and store version string on my "app" reducer. But I do not feel this is an ideal solution and am curious what other strategies might ease migrations.

As for 2, you might consider sending the persistor down via context. Again it is not something I have done, and I am curious to hear how it works out for you. Good luck!

from redux-persist.

pke avatar pke commented on August 15, 2024

As for resetting the state to initial, isn't there a standard redux way to do that? After all, redux does get a fresh state on app start, by sending an internal INIT action to the store, which results in a call to reducers with state set to undefined. That causes all reducers to send back their initial state.

See here: http://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store/35641992#35641992

from redux-persist.

rt2zz avatar rt2zz commented on August 15, 2024

After using redux more, my recommendation for getting access to purge is to expose the persistor via context. Then you can call this.context.persistor.purgeAll from anywhere in your application.

The behavior of purge is to clear the storage but not reset state, so a page reload or a RESET action will still be required.

There are other approaches possible such as a custom action creator that has the persistor in scope.

If changes are needed in the redux-persist lib to support specific use cases let me know and we can evaluate feature additions.

from redux-persist.

rt2zz avatar rt2zz commented on August 15, 2024

note for posterity there is now a purgeStoredState top level export that can help purge state without having access to the persistor. Resetting redux state is still out of scope of this module, but IIRC there are a couple of other packages that solve that very problem.

from redux-persist.

lucianomlima avatar lucianomlima commented on August 15, 2024

@rt2zz in V4 with purgeStoredState is possible to select store to purge or in a deep way, a data to be purged in a store?

from redux-persist.

 avatar commented on August 15, 2024

@lucianomlima , it probably can be done with PURGE. Have you cleared your store, please share how did you do this.

from redux-persist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.