How to write useMutation hook with onCompleted as a parameter?

280 views Asked by At

I need to write dedicated hook useMutation with onCompleted and parameter onMutationCompleted.

In file it uses like:

const [data] = useClearWorkerMutation({ onCompleted: onMutationCompleted });

I don't know how to put onCompleted in this hook correctly.

export function useClearWorkerMutation() {
  return useMutation<ClearWorkerMutationData, ClearWorkerMutationInput>(
    CLEAR_WORKER_MUTATION,
    {
      onCompleted: (data) =>
      },
    },
  );
}
1

There are 1 answers

0
Michel Floyd On

Per the docs the function signature for useMutation is:

function useMutation<TData = any, TVariables = OperationVariables>(
  mutation: DocumentNode,
  options?: MutationHookOptions<TData, TVariables>,
): MutationTuple<TData, TVariables> {}

If the only option is onCompleted and you have some query then you would have:

useMutation(YOUR_MUTATION,{onCompleted: (data) => {…your function…})

It looks as if you're doing this correctly. Are you getting errors? Is your onCompleted function not getting executed? What's not working?