TanStackT
TanStack8mo ago
6 replies
colossal-harlequin

default mutation onError fallback?

I would like to create a global useMutation error fallback, where it runs only if the specific mutation doesn't have its own onError handler, or if that error throws. I have set defaultOptions in QueryClient as so:
const queryClient = new QueryClient({
  defaultOptions: {
    mutations: {
      onError: (error) => {
        console.error("Error:", error);
      },
    },
  },
});

having this mutation:
  const addMutation = useMutation(
    trpc.iot.addNotificationContact.mutationOptions()
  );
    addMutation.mutate(res, {
      onError: (error) => {
        console.error("Failed to add notification contacts:", error);
      },
    });

both onError handlers are running. I would like addMutation to be the only one running.
Was this page helpful?