Remove query once logged out
I am having problem on how to remove a query that gets a user data once logout.
I want to find a way to force a reload/refetch when I log in as the previous data is shown for a second before changing the details of the new user and is not great. Below my logout logic in mobx store to delete the token

11 Replies
deep-jadeOP•3y ago
this is my login mutation

deep-jadeOP•3y ago
and here the query to get the user

deep-jadeOP•3y ago
Please I need to understand how I can reload or refetch, even remove the user data once logout
foreign-sapphire•3y ago
Would you be able to provide a sandbox with a minimal reproduction of the issue?
deep-jadeOP•3y ago
@julien I'm not sure if it is possible as I use a .net api as back end
But I'll post a small video of what is happening
stuck-chocolate•3y ago
Clearing the query cache, removing, invalidating and refetching queries should all work here. What's the problem exactly?
deep-jadeOP•3y ago
@Louis well once I log out, the user query get disabled but not cancelled. When I log in again, the data from previous user shows up for a second before refetching and getting the current user. So, I'd like to remove the query once log out in order to get straight fresh data with the current user
foreign-sapphire•3y ago
I see you're calling
resetQueries on login, doesn't that clear the cache? What if you move that up and do it before setting the token in the store, or do it on logout?
Alternatively you could include in the query keys something that changes between sessions (like a session id) so that the cache is not reused across sessions.deep-jadeOP•3y ago
@julien I've tried that resetQueries but is not working, I am sure I tried it first before to set the token too but didn't work.
Would be good instead to create another hook mutation to logout, use the logout function from the store and in there remove the query? Although that function is just synchronous but the removeQuery isn't that I think could be the way?
Or better , I can delete the logout function and do it in the mutation the cancellation of the token from the local storage and remove the query😃
I'm thinking and writing my thoughts
foreign-sapphire•3y ago
Resetting the query when logging out seems like a good idea indeed. If the logout function is synchronous, you don't necessarily need a mutation, you can just call it and then call
queryClient.resetQueries.deep-jadeOP•3y ago
@julien thanks that's what makes react query awesome, simplicity
I've fixed it with just adding onMutate, removeQueries when logging in. In this case, before the logging in the query to get the user get removed and re populated once is successful so I get straight the new user😃
The onSuccess onError and onMutate are the most powerful tool in react query I love it