[英]How can I initiate a new function, immediately after a function is successful in react native
I want to run a function, just after a function is completed, and also pass some data into the second function.我想运行一个函数,就在一个函数完成后,还要将一些数据传递给第二个函数。 I have tested the two functions individually, they both work.我分别测试了这两个功能,它们都可以工作。 But I want to initiate the second one immediately after the first is complete但我想在第一个完成后立即启动第二个
This is my code below but the second function didn't run properly.这是我下面的代码,但第二个函数没有正常运行。
const ChargeCard = (dispatch) => ({
cardNumber,
expiryMonth,
expiryYear,
cvc,
email,
amountInKobo,
id
}) => {
try {
RNPaystack.chargeCard({
cardNumber: cardNumber,
expiryMonth: expiryMonth,
expiryYear: expiryYear,
cvc: cvc,
email: email,
amountInKobo: amountInKobo * 100
})
.then(response => {
if (response) {
setBasic({ id }) //the function to run if this is successful
}
})
} catch {
dispatch({
type: "ADD_ERROR",
payload:
"Something went wrong during payment, please try again, don't worry you have not been charged."
});
}
};
This function I want to run immediately that is successful;这个功能我想立即运行即成功;
const setBasic = () => async ({ id }) => {
const Basic = 1
const response = await appApi.put(`/usersub/${id}`, { Basic });
if (response) {
console.log(response.data)
}
}
Change the second function to this:将第二个函数更改为:
const setBasic = async ({ id }) => {
const Basic = 1
const response = await appApi.put(`/usersub/${id}`, { Basic });
if (response) {
console.log(response.data)
}
}
You've defined it as:您已将其定义为:
const setBasic = () => async ({ id }) => {
but you are calling it with:但你用以下方式调用它:
setBasic({ id })
why mix async await
with then()
stick with one and be consistent with it.为什么将async await
与then()
混合使用并保持一致。
const setBasic = async ({ id }) => {
const Basic = 1
const response = await appApi.put(`/usersub/${id}`, { Basic });
if (response) {
console.log(response.data)
}
}
const ChargeCard = (dispatch) => async ({
cardNumber,
expiryMonth,
expiryYear,
cvc,
email,
amountInKobo,
id
}) => {
try {
const response = await RNPaystack.chargeCard({
cardNumber: cardNumber,
expiryMonth: expiryMonth,
expiryYear: expiryYear,
cvc: cvc,
email: email,
amountInKobo: amountInKobo * 100
})
if(response) setBasic({ id }) //the function to run if this is successful
} catch {
dispatch({
type: "ADD_ERROR",
payload:
"Something went wrong during payment, please try again, don't worry you have not been charged."
});
}
};
This way your login is consistent.这样您的登录名就保持一致。 However, I just want to mention this;不过,我只想提一下; your first function looks like a shorthand way to write action creators using redux-thunk
in case I am right, I think you are passing the dispatch
wrongly in the first function.您的第一个函数看起来像是action creators using redux-thunk
编写action creators using redux-thunk
的速记方式,以防万一,我认为您在第一个函数中错误地传递了dispatch
。 You should rewrite your function to this:您应该将您的函数重写为:
const ChargeCard = ({
cardNumber,
expiryMonth,
expiryYear,
cvc,
email,
amountInKobo,
id
}) => async dispatch => {
try {
const response = await RNPaystack.chargeCard({
cardNumber: cardNumber,
expiryMonth: expiryMonth,
expiryYear: expiryYear,
cvc: cvc,
email: email,
amountInKobo: amountInKobo * 100
})
if(response) setBasic({ id }) //the function to run if this is successful
} catch {
dispatch({
type: "ADD_ERROR",
payload:
"Something went wrong during payment, please try again, don't worry you have not been charged."
});
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.