-
Notifications
You must be signed in to change notification settings - Fork 862
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
[V5] How to purge when I want to clear the storage cache in ReactNative? #579
Comments
Hi @landpy, did you figure this out ? |
You should be able to dispatch a Reducer:
Test Component:
Wire in the reducer and test component into your app and you should (hopefully) find when you click the button, the store is cleared and messages are logged to the console. |
@landpy apologize for taking so long to get around to this. There are a few approaches here depending on your needs.
|
@rt2zz i landed here accidentally after hours of google searching on why redux-persist wasn't persisting to my local storage a state change in session reducer after user was logged out. During logged out i just cleared the session to the initial state (without mutating of course), but redux-persist didn't appear to persist the newly blank values into local storage and hence... on a refresh the auto rehydrate would bring back the values as if the user was still logged in. |
@tommyalvarez Hi Tommy, I'm facing the same situation as you. I'm clearing the session on log out. Did you end up just calling |
@Kevin-Do Yup, that did the trick. Everything's working like a charm now. I clean the session in redux + purge, because purge will only clean the localstorage. |
@tommyalvarez Awesome, thanks! |
you can use this: and anywhere you want: |
After more googling and checking the redux-persist documentation (on v5), i updated my project following strictly how to initiaize this library and i did not have to manually purge nothing else anymore. It's now working out of the box for me. So guideline rule: If you are manually calling purge or persist, or triggering action creators of the lib by hand... you're doing something wrong setting it up. |
@tommyalvarez could you provide an example showing how you're setting up everything? Currently I'm facing the exact same issue you were having and I thought calling the |
@bruno-edo sure, here it goes: main.js => Where all starts in my client-side app
createStore.js:
reducers.js:
Hope it helps you! |
If anyone is interested - here is Dan Abramov's solution - though it's not specific to redux-persist: |
I'm using this before my app is rendered for debugging purposes. If something gets messed up in my localStorage, I can reset it by visiting /?debug
|
from where did u import persistor and purge?? |
persistor is returned from persistStore. So example:
|
Why isn't this documented better and easier to find? It is very difficult to find this specific solution of calling purge() and I'm sure its a common requirement for developers? |
Thanks so much! |
Hi rt2zz~
I want to click one button to clear the storage cache in ReactNative, I used PersistGate but I don't know how to invoke the purge method in the RN component. Could you please explain how to invoke purge method when the onPress event occurs?Thx a lot!!
The code is as following:
import * as React from 'react';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/es/integration/react';
import IntlComponent from './IntlComponent';
import configureStore from './middlewares';
import {View,Text} from 'react-native';
const { persistor, store } = configureStore();
const onBeforeLift = () => {
// take some action before the gate lifts
}
export default class App extends React.Component {
render() {
return
<PersistGate
onBeforeLift={onBeforeLift}
persistor={persistor}
loading={}>
}
}
The text was updated successfully, but these errors were encountered: