useDispatch hook triggered in the wrong order,

Viewed 134

I'm just learning Redux and I have a problem.

I have actions:

export const getValues = () => async (dispatch) => {
    try {
        const res = await axios.get(`/values`);
        dispatch({
            type: VALUE_STATE,
            payload: res.data,
        });
    } catch (err) {
        console.log(err);
    }
};
    
    
export const getUsers = () => async (dispatch) => {
    try {
        const res = await axios.get(`/users`);
        dispatch({
            type: ALL_USER,
            payload: res.data,
        });
    } catch (err) {
        console.log(err);
    }
};

COMPONENT

const handleClick = () => {
    dispatch(getValues());
    dispatch(getUsers());
};

Why, when I dispatch, they are triggered in the wrong order, sometimes in the right order ?

1 Answers

use batch to set them by order in one state update:

const handleClick = () => async {
     await getValues();
     await getUsers();
};
Related