0

I am using onError callback to show toast messsage globally. And I also want to clear all cache If user gets 401. But in error callback error and query parameters don't have any method to achieve this. How can I do it?

const queryClient = useMemo(
    () =>
      new QueryClient({
        queryCache: new QueryCache({
          onError: (error, query) => {
            handleToastErrorMessage(error);

            if (error.response.status === 401) {
              // I want to remove all queries from cache here
              // removeQueries();
            }
          },
        }), []);
Furkan
  • 85
  • 1
  • 7

1 Answers1

0

Just use queryClient variable that you just have and its method queryClient.clear() will clear all connected caches.

Although you might want to use queryClient.resetQueries() instead to reset queries in the cache to their initial state, or even queryClient.invalidateQueries() to just refetch everything? It's up to you though.

All the info is in the docs as always

Danila
  • 15,606
  • 2
  • 35
  • 67
  • I want to clear cache inside of new QueryClient's on error callback. How can I use queryClient inside of it? It hasn't created yet. It will be created after QueryClient returns. – Furkan Aug 09 '23 at 20:25
  • 1
    You answered yourself, you can use it because it will be created at that point when something calls `onError` – Danila Aug 09 '23 at 21:07