dispatch不是函数,async

时间:2017-11-29 08:59:58

标签: react-native redux redux-thunk

我在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");
    });
};

1 个答案:

答案 0 :(得分:2)

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");
    });
};