Home > Net >  isSuccess for mutations in RTK Query
isSuccess for mutations in RTK Query

Time:01-01

I'm calling the mutation method as usual,

const [addTodo] = useAddTodoMutation();

So we have options like isLoading,isSuccess,isError,error from a query builder (From GET requests). But Can't we have the same options with mutations too?

CodePudding user response:

It's already there :)

For query hooks, the return value is an object containing data and the various loading/status flags: const { data, isFetching} = useSomeQuery().

For mutation hooks, the return value is a tuple containing the "trigger" function as the first entry, and an object containing the status flags as the second entry: const [trigger, objectWithStatusFlags] = useSomeMutation():

So, just extract that object (and optionally destructure the fields from it):

// Either this:
const [addTodo, mutationFlags] = useAddTodoMutation();

// or this:
const [addTodo, {isLoading}] = useAddTodoMutation();
  • Related