gpt4 book ai didi

reactjs - useDispatch Hook 以错误的顺序触发,

转载 作者:行者123 更新时间:2023-12-04 03:47:21 25 4
gpt4 key购买 nike

我刚开始学习 Redux,但遇到了问题。

我有 Action :

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

组件

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

为什么,当我调度时,它们以错误的顺序触发,有时以正确的顺序触发?

最佳答案

使用批处理在一次状态更新中按顺序设置它们:

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

关于reactjs - useDispatch Hook 以错误的顺序触发,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64946172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com