I want to use invalidate queries but it doesnt fetch it or trigger it.
I have a dashboard and I am on the user screen on the navigation. I mutate from the user screen an action and want to refrech the dashboard which is in other file.
user: (Here on onSuccess is the invalidateQueries thats not trigger)
const {
isPending,
mutate
} = useMutation({
mutationKey: ["SETTINGS"],
mutationFn: async (params: TMutateSettingsParam) => {
console.log(params);
const res = await axiosPrivate.post(`${URL}/auth/deleteAccountByAdmin`, params, {
headers: headersJSON,
withCredentials: true
});
const data: IReturnApi<null> = res.data;
return data;
},
onSuccess: (data) => {
queryClient.invalidateQueries({queryKey: ['dashboard']})
toast.success(data.message);
},
onError: (err: AxiosError<IReturnApi<IValidationError[]>>, _variables) => {
if(err.response) {
toast.error(err.response.data.message);
} else {
toast.error('Es ist ein Fehler aufgetreten. Bitte wenden Sie sich an den Support.');
}
}
});
Here is my app.tsx
export const queryClient = new QueryClient();
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<BrowserRouter>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<App />
</AuthProvider>
</QueryClientProvider>
</BrowserRouter>
</React.StrictMode>,
)
And here is my dashboard query which I want to invalidate:
const {
isLoading,
isError,
data,
error
} = useQuery({
queryKey: ['dashboard'],
refetchOnWindowFocus: false,
queryFn: async () => {
const res = await axiosPrivate.get(`${URL}/dashboard/getAll`, {
headers: headersJSON,
withCredentials: true
});
const data: IDashboardData = await res.data;
return data;
}
});
What I am doing wrong ?
New contributor
wepro01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.