I am trying to learn react.js with @tanstack/react-query and so far things are working fine.
However, I am trying to use invalidateQueries to clear the existing cache after insert, update, etc. to fetch the fresh data from the server. But, its not working.
I have already tried various solutions found after googling, but still not success.
main.tsx
import React from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
...
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 10 * (60 * 1000), // 10 mins
refetchOnMount: false,
},
},
});
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
...
</QueryClientProvider>
</React.StrictMode>,
);
edit.tsx
const update = async (data: any) => {
...
};
export default () => {
const queryClient = useQueryClient();
...
const { mutate, isLoading } = useMutation({
mutationFn: update,
onError: ({ response }) => setErrors(showErrors(response.data)),
onSuccess: () => {
queryClient.invalidateQueries([modules.accountTypes.cacheKey]);
navigate(PATH);
},
});
...
};
modules.accountTypes.cacheKeymodules.accountTypes.cacheKey= 'accountTypes'