-
Notifications
You must be signed in to change notification settings - Fork 866
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 work #1015
Comments
I was confused by this too - not sure if you fixed it by now, but for any people who stumble across this in the future: in order for this to work you have to make sure there is a PURGE action for each reducer that returns your 'clean' state. Then your commands of purge(), flush() will clear and force save the changes. For reference I am coding with react-native (so I assume this is the same for react) what's confusing is that if you immediately call purge() after setting up the persistor in store.js it will clear it on boot as expected |
@Trashpants, can you elaborate on what a PURGE action is for each reducer? Is the data being erased from the disk, but not in redux? I cannot tell if I'm running into the same issues you are describing. thanks in advance |
When I run
response comes back as |
@chiaberry I have multiple reducers implemented with a PURGE case as
that way when I call persistor.purge() its actually setting my state back to |
@Trashpants awesome! thank you |
I was having a similar issue, but after I implemented
|
And I am getting |
@lolz42 that is expected. The function does not return anything besides a promise. redux-persist/src/persistStore.js Line 95 in f6f6d64
|
and then depends on your app logic resume it by e.g in login page ? |
i have the same problem. when i upgrade the app
not work for me. |
I ran into a similar situation and need to enable remote debugging to have |
works for me! |
Works for me: const purge = () => {
persistor.purge()
console.log("Factory reset performed.")
}
<button onClick={() => purge()} >Purge</button> Does NOT work for me: const purge = () => {
persistor.purge()
console.log("Factory reset performed.")
}
purge() Also does NOT work for me: const purge = async () => {
await persistor.purge()
console.log("Factory reset performed.")
}
purge() Solution from @titulus also works for me const purge = async () => {
await persistor.purge()
await persistor.purge()
console.log("Factory reset performed.")
}
purge() Alternative solution also works for me: const purge = () => {
persistor.purge()
console.log("Factory reset performed.")
}
setTimeout(() => purge(), 200) I played around with the delay and it seems to need at least 100ms. |
I just did setTimeout(() => persistor.purge(), 200) and works great. |
in all seriousness, I think I found the fix. You all want me to submit a PR?
|
Strangely it's working with a
|
I don't understand most of the comments. persistore.purge() as it's clearing localStorage has to be a promise / async. You can await it inside an async function and I'd use a try/catch as well: export let logout = () => async (dispatch) => {
try {
await persistor.purge();
dispatch(logoutSucceed());
} catch (err) {
dispatch(logoutFailed(err));
}
}; I assume just doing |
I am implement
|
I was faced this kind of problem yesterday, and keep finding the solution across Google / Stack Overflow. And below is my solution that works fine now for people who is stilling struggling right now. My problem that I need to get through yesterday is my redux-persist is not working after my rootReducer reset by const logoutEffect = () => {
const {error, success} = authState.remotes.logout;
if (error || success) {
dispatch(logoutReset());
persistor.pause();
persistor.flush().then(() => {
return persistor.purge();
});
// navigation.navigate('Welcome');
navigation.dispatch(
CommonActions.reset({
index: 1,
routes: [{name: 'Welcome'}],
}),
);
persistor.persist();
}
}; |
Lol. Read the docs. It’s a callback, not a promise. Add a timeout and just do the purge after 1 second.
… On May 20, 2021, at 21:24, chungmarcoo ***@***.***> wrote:
I was faced this kind of problem yesterday, and keep finding the solution across Google / Stack Overflow. And below is my solution that works fine now for people who is stilling struggling right now. My problem that I need to get through yesterday is that my redex-persist is not working atfer my rootReducer reset by return rootReducer(undefined, action);. And here is my solution in my LogoutScreen (i.e. in stack navigation), and my store.js no need to change the original code (i.e. return rootReducer(undefined, action);)
`
const logoutEffect = () => {
const {error, success} = authState.remotes.logout;
if (error || success) {
dispatch(logoutReset());
persistor.pause();
persistor.flush().then(() => {
return persistor.purge();
});
navigation.dispatch(
CommonActions.reset({
index: 1,
routes: [{name: 'Welcome'}],
}),
);
persistor.persist();
}
};
`
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
The timeout is courtesy of rt2zz/redux-persist#1015
The timeout is courtesy of rt2zz/redux-persist#1015
credit to @Trashpants for the hint, but for those of my fellow Redux Toolkit fans, you need to add an 'extraReducer' on your slice.
|
This example is for those who use You must reload your application, this is the whole solution.
This works great. |
This method is applicable for those who use Redux Toolkit. builder.addCase(PURGE, () => initialState); Example import { PURGE } from "redux-persist";
import { AuthUser } from "../types/user";
import { createSlice } from "@reduxjs/toolkit";
export interface AuthState {
user: AuthUser | null;
token: string | null;
}
const initialState: AuthState = {
user: null,
token: null,
};
const slice = createSlice({
name: "auth",
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(PURGE, () => initialState); // THIS LINE
},
}); Purge Usage: persistor.purge().then(() => /** whatever you want to do **/); |
@suleymanozev thank you thousand times and more kind sir. Saved me :) |
Whe is the customEntityAdapter importing from? |
|
import { persistor } from "../../../../reduxToolkit/store"; const clearPersistData=()=>{ this worked for me !!!! @chungmarcoo thanks for the help |
// To clear persisted state data and immediately flush the storage system: |
Unless it's been updated, the purge is not a promise, so that will not
work. Any positive test cases are just my coincidence.It is a callback and
must be handled by passing a function which will execute asynchronously. If
you read the source code it will confirm this.
…On Sun, Feb 19, 2023 at 11:10 AM Probir Sarkar ***@***.***> wrote:
// To clear persisted state data and immediately flush the storage system:
persistor.purge().then(() => persistor.flush());
—
Reply to this email directly, view it on GitHub
<#1015 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEFV3ACHIOIUSP6E4QU7XDWYJHY3ANCNFSM4HB7IJOQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
This source indicates its a promise: redux-persist/src/persistStore.ts Line 91 in d8b01a0
And it seems to have been for the past 6 years: |
Hello. Using persistor.purge doesn't clear any data in my storage.
Here's the code:
getPersistor() function returns persistor, and I called purge function but still nothing changed in my storage.
I also tried this code as well, but doesn't worked either:
It seems like purge doesn't work. Used @5.10.0.
The text was updated successfully, but these errors were encountered: