如何在反应查询中进行依赖突变?
How to make a dependent mutation in react query?
我有一个 mutate 函数,它发出 POST 请求并接收 id
,之后我需要立即调用下一个 mutation 函数根据这个 id
进行购买,如何我做这个吗?
const mutation = useCreatePaymentMethodMutation();
const handleSubmit = async(values:any) {
const form = await mutation.mutateAsync({
values,
hash
})
return form
}
useCreatePaymentMethodMutation
是从@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
这是我的usePayMutation
方法
const usePayMutation = () => {
return useMutation(({ data, id }: { data: any; id: string }) =>
payRequest(data, id)
);
};
你会创建两个不同的突变,并一个接一个地称呼它们:
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(...)
}
您有 2 个选择
选项 1:
在你的 useCreatePaymentMethodMutation
钩子中
const useCreatePaymentMethodMutation = () => {
const mutatePay = usePayMutation()
return useMutation(
({ values, accessHash }: { values: any; accessHash: string }) =>
createPaymentMethodRequest(values, accessHash),
{
onSuccess: (data) => {
mutatePay(data.id)
},
}
);
}
选项 2:
在您的 handleSubmit
函数中
const mutatePay = usePayMutation()
const handleSubmit = (values:any) => {
return mutation.mutateAsync({
values,
hash
}).then(data=> mutatePay(data.id))
}
我有一个 mutate 函数,它发出 POST 请求并接收 id
,之后我需要立即调用下一个 mutation 函数根据这个 id
进行购买,如何我做这个吗?
const mutation = useCreatePaymentMethodMutation();
const handleSubmit = async(values:any) {
const form = await mutation.mutateAsync({
values,
hash
})
return form
}
useCreatePaymentMethodMutation
是从@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
这是我的usePayMutation
方法
const usePayMutation = () => {
return useMutation(({ data, id }: { data: any; id: string }) =>
payRequest(data, id)
);
};
你会创建两个不同的突变,并一个接一个地称呼它们:
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(...)
}
您有 2 个选择
选项 1:
在你的 useCreatePaymentMethodMutation
钩子中
const useCreatePaymentMethodMutation = () => {
const mutatePay = usePayMutation()
return useMutation(
({ values, accessHash }: { values: any; accessHash: string }) =>
createPaymentMethodRequest(values, accessHash),
{
onSuccess: (data) => {
mutatePay(data.id)
},
}
);
}
选项 2:
在您的 handleSubmit
函数中
const mutatePay = usePayMutation()
const handleSubmit = (values:any) => {
return mutation.mutateAsync({
values,
hash
}).then(data=> mutatePay(data.id))
}