Search code examples
react-nativereduxredux-thunk

dispatch is not a function, async


I have the following action in a react-native application. I receive the error "dispatch is not a function. I am new to redux and struggle with async actions. Is there a way to set the Asyncstorage to the state without using async/await?

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

Solution

  • balanceCall should be returning a function that takes dispatch as an argument. change the code accordingly .

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