TanStackT
TanStack8mo ago
1 reply
standard-azure

Reset Mutation State in onError Handler of MutationCache

Hello, guys!
It’s possible to reset the mutation state inside the global onError handler for MutationCache?
I want to catch an AuthExpiredError from my API (when the user’s session has expired), in order to reset the state of all active queries and mutations, and then show a login modal.

Something like this:
{
  queryClient: new QueryClient({
    queryCache: new QueryCache({
      onError(error, query) {
        if (error instanceof AuthExpiredError) {
          query.reset();
          displayLogInForm();
        }
      },
    }),
    mutationCache: new MutationCache({
      onError(error, variables, context, mutation) {
        if (error instanceof AuthExpiredError) {
          mutation.reset(); /// how to reset mutation state? 🤔
          displayLogInForm();
        }
      },
    }),
  }),
}
Was this page helpful?