11

I have a mutate function which make POST request and receives id , after then I need Immediately call next mutation function to make a purchase in accordance with this id, how do I make this ?

const mutation = useCreatePaymentMethodMutation();

const handleSubmit = async(values:any) {
  const form = await mutation.mutateAsync({
    values,
    hash
  })
  return form
}

useCreatePaymentMethodMutation is imported from @Hooks

const useCreatePaymentMethodMutation = () => {
 return useMutation(
    ({ values, accessHash }: { values: any; accessHash: string }) =>
      createPaymentMethodRequest(values, accessHash),
    {
      onSuccess: (data) => {
        // usePayMutation(data.data, accessHash); error. I need to use data after 1st mutation in next 
        request
        console.log("on success data ", data);
      },
    }
  );
}

UPD 1

Here is my usePayMutation method

const usePayMutation = () => {
  return useMutation(({ data, id }: { data: any; id: string }) =>
    payRequest(data, id)
  );
};

2 Answers 2

7

There're 2 options for you to do it

Option 1: In your useCreatePaymentMethodMutation hooks

const useCreatePaymentMethodMutation = () => {
 const mutatePay = usePayMutation()
 return useMutation(
    ({ values, accessHash }: { values: any; accessHash: string }) =>
      createPaymentMethodRequest(values, accessHash),
    {
      onSuccess: (data) => {
        mutatePay(data.id)
      },
    }
  );
}

Option 2: In your handleSubmit function

const mutatePay = usePayMutation()

const handleSubmit = (values:any) => {
  return mutation.mutateAsync({
    values,
    hash
  }).then(data=> mutatePay(data.id))
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have the following error An unhandled error was caught from submitForm() TypeError: Cannot read properties of undefined (reading 'status')
in 1st option This expression is not callable. Type 'UseMutationResult<AxiosResponse<any> | { isError: boolean; status: any; }, unknown, { data: any; id: string; }, unknown>' has no call signatures.
7

you would create two different mutations, and call them one after the other:

const createPayment = useCreatePaymentMethodMutation()
const otherMutation = useOtherMutation()

const handleSubmit = async(values:any) {
  try {
    const form = await createPayment.mutateAsync({
      values,
      hash
    })
    await otherMutation.mutateAsync(form.id)
  }
  catch(...)
}

4 Comments

Cannot read properties of undefined (reading 'status') when i log form it is an object { config, data, headers, request , status, statusText } so I was trying to put in otherMutation form.data
if i put whole form , otherMutation.mutateAsync(form) then Argument of type 'AxiosResponse<any> | { isError: boolean; status: any; }' is not assignable to parameter of type '{ data: any; id: string; }'.
thanks for the reply. do you have to try catch the result of a mutation? it has a lot of nice error handling features like onError, isError, onSettled etc.
mutateAsync is exactly what I needed! thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.