Replies: 6 comments 7 replies
-
you'd want to invalidate the query with |
Beta Was this translation helpful? Give feedback.
-
We're having the same issues here. |
Beta Was this translation helpful? Give feedback.
-
What I'm seeing is that in the following case the detail query is not being removed from the cache when const apiUrl = 'api/qualification';
const queryKeys = {
all: ['qualifications'] as const,
lists: () => [...queryKeys.all, 'list'] as const,
list: (filters: string) => [...queryKeys.lists(), { filters }] as const,
details: () => [...queryKeys.all, 'detail'] as const,
detail: (id: string) => [...queryKeys.details(), id] as const,
};
const deleteQualification = async (uid: string) => {
const { data } = await api.delete<void>(`${apiUrl}/${uid}`);
return data;
};
export const useDeleteQualification = () => {
const queryClient = useQueryClient();
return useMutation(deleteQualification, {
onSuccess: (_data, uid) => {
queryClient.removeQueries(queryKeys.detail(uid));
queryClient.setQueriesData(queryKeys.lists(),
(previous: QualificationListItem[] | undefined) => previous?.filter((item) => item.uid !== uid) ?? []);
}
});
}; |
Beta Was this translation helpful? Give feedback.
-
Did anyone find a solution for this one? |
Beta Was this translation helpful? Give feedback.
-
@jmz7v here's the solution. To use Your code should look like this // other imports
import { useQueryClient } from '@tanstack/react-query'
const Component = () => {
const queryClient = useQueryClient()
const removeQuery = () => {
queryClient.removeQueries( queryKey )
} I hope this helps. @TkDodo maybe the documentation needs updating or the library needs a fix, either ways this is what worked for me. |
Beta Was this translation helpful? Give feedback.
-
I found this thread searching for a way to clear/reset the cache and re-issue the active queries and none of the functions here worked for my case. However resetQueries did exactly what I was looking for:
From on the docs on resetQueries:
|
Beta Was this translation helpful? Give feedback.
-
I have a page that displays data that user can edit. Once they send the data, most of the time the data is updated to the server correctly and the data on the page is updated. Sometimes this doesn't happen and it will still render the old data until the cache time runs out.
This is the query that's cache needs to be removed:
This is the code that handles sending the updated data and removing the cached query:
I have also tried
invalidateQueries
andresetQueries
. Most commonly this seems to happen if you make new update to data too soon after previous update. The data always updates on server correctly, but the client data doesn't always update because the cache isn't clearing. Once the cache time runs out and I update the page the data is corrected on the page. Is there something wrong with how I'm handling this or is this a bug?Beta Was this translation helpful? Give feedback.
All reactions