Comments (8)
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
- expose persistor via context similar to how react-redux Provider works
- pass the persistor down via props
- 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.
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.
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.
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.
from redux-persist.
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.
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.
@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.
@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)
- Jest snapshot is always created as null in react native
- Persist not working after page reload or refresh HOT 4
- failed to preserved initial state value at rehydrate
- PersistGate in SSR applications rendering app twice when using React v18 HOT 3
- Documentation for Migration is unnecessary complicated. HOT 1
- Getting got unexpected undefined error while changing redux state HOT 1
- getting error: A non-serializable value was detected in an action HOT 6
- createStore() is old HOT 1
- Clear Redux Persist Data on App uninstall in IOS React Native HOT 1
- Support for Redux v5 and RTK v2 HOT 10
- state is null HOT 1
- data migration security and automatization
- Redux persist not working with RN 0.73 HOT 12
- Move project to new repo? HOT 13
- redux-persist failed to create sync storage. falling back to noop storage | Next js HOT 3
- Vitest - The slice reducer for key "reducer name" returned undefined during initialization
- Option to disable storage creation error for dynamic stores
- storage.getItem is not a function (it is undefined) - RN 0.73.4 HOT 4
- Redux persist getting cleared after persisting PatientsReducers
- Dead link for config object of persistReducer function.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-persist.