简体   繁体   English

如何在反应原生功能成功后立即启动新功能

[英]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 awaitthen()混合使用并保持一致。

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.

相关问题 React Native:如何从函数创建新的 Text 组件? - React Native: How can I create a new Text component from a function? 如何使 function 中的一部分代码在 state 更新后运行,或者在本机反应中未定义时运行? - How can I make a part of code inside the function to run after the state updates or when it is not undefined in react native? 如何在另一个功能完成后立即触发一个功能? - How do I fire a function immediately after another function is finished? 我如何从 React Native 中的辅助函数分派动作 - How can i dispatch an action from a helper function in react native 如何在 React Native 上更正我的注销 function? - How can I correct my logout function on React Native? 如何在本机反应中将无状态 function 转换为 class 组件 - How can I convert stateless function to class component in react native 我如何过滤地图功能以使用搜索栏做出本机反应? - How can i filter map function in react native with searchbar? 如何在量角器(使用茉莉花)中实现阻塞等待以调用异步函数,并在函数返回后立即使测试失败? - How can I implement a blocking wait in protractor (with jasmine) to call an async function, and fail the test immediately after the function returns? 如何在不立即调用此函数的情况下将其添加到数组? - How can I add this function to an array without it being called immediately? 如何防止该函数立即执行? - How can I prevent the function from beeing executed immediately?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM