【问题标题】:dispatch is not a function, asyncdispatch 不是函数,异步
【发布时间】:2018-05-12 22:07:19
【问题描述】:

我在 react-native 应用程序中有以下操作。我收到错误消息“dispatch 不是函数。我是 redux 新手,并且在处理异步操作时遇到了困难。有没有办法在不使用 async/await 的情况下将 Asyncstorage 设置为状态?

export const balanceCall =async (dispatch) => {

let token = await getAsyncStoreT();
let mobile= await getAsyncStoreM();


balanceURL = `https://localhost/v1/user/find?token=${token}&mobile=${mobile}`;

console.log(balanceURL);
axios({
    method: 'get',
    url: checkURL,
}).then(function (response) {
    let balance =response.data.map(user=>user.balance);
    dispatch(balanceReceived(balance));
})
    .catch(function (error) {
        console.log(error);
        console.log("NOT AGAIN");
    });
};

【问题讨论】:

    标签: react-native redux redux-thunk


    【解决方案1】:

    balanceCall 应该返回一个以 dispatch 作为参数的函数。相应地更改代码。

    export const balanceCall = () => async dispatch => {
    
    let token = await getAsyncStoreT();
    let mobile= await getAsyncStoreM();
    
    
    balanceURL = `https://localhost/v1/user/find?token=${token}&mobile=${mobile}`;
    
    console.log(balanceURL);
    axios({
        method: 'get',
        url: checkURL,
    }).then(function (response) {
        let balance =response.data.map(user=>user.balance);
        dispatch(balanceReceived(balance));
    })
        .catch(function (error) {
            console.log(error);
            console.log("NOT AGAIN");
        });
    };
    

    【讨论】:

    • 感谢您的帮助。它似乎解决了错误,但调度从未发生。你有什么建议吗?
    猜你喜欢
    • 2016-12-19
    • 2019-09-04
    • 2020-09-13
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2020-10-20
    • 1970-01-01
    相关资源
    最近更新 更多