TanStackT
TanStack12mo ago
12 replies
urgent-maroon

useMutation onError never triggered?

I am experiencing an issue with
useMutation
in my custom hook. The
onError
logic is never getting triggered, the
onSuccess
is. The hook (throws on error 1/3, random):

import { useMutation } from '@tanstack/react-query';

const SONGS = ['Eine Woche wach', 'Ich bin solo'];

const sing = async () => {
  if (Math.random() < 1 / 3) {
    throw new Error('Mickie Krause forgot the lyrics!');
  }
  return SONGS[Math.floor(Math.random() * SONGS.length)];
};

const useMickieKrause = () => {
  return useMutation({
    mutationFn: sing,
    onSuccess: (song) => {
      console.log(`Mickie Krause is singing: ${song}`);
    },
    onError: (error) => {
      console.log(`Mickie Krause had an error: ${error}`);
    },
  });
};

export { useMickieKrause };


I only see Mickie Krause is singing... messages. Any help would be appreciated.
Was this page helpful?