我有一个redux问题。下面是我的代码:
actions.ts
export const getOrders = (id: string) => {
return async (dispatch) => {
const queryParams = {
id,
search: null,
order: "DESC",
status: "all"
};
const orders = await api.get('/api/orders', queryParams);
dispatch(getOrdersSuccess(orders));
}
}
export const getOrdersSuccess= (payload) => {
return {
type: 'GET_ORDERS_SUCCESS',
payload
};
}
someComponent.ts
useEffect(() => {
dispatch(getOrders(id)); //here is problem
}, [dispatch, id])
谁能告诉我为什么我不能在dispatch
内使用getIncidents(id)
函数?当我输入这个函数时,VSCode会给我这个错误:
Argument of type '(dispatch: any) => Promise<void>' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type '(dispatch: any) => Promise<void>' but required in type 'AnyAction'.ts(2345)
我不知道如何解决这个问题:/
谢谢你的帮助!
尝试等待promise
解决,然后dispatch
您的操作:
async/await
:
useEffect(() => {
(
async function handleAsync() {
let order = await getOrders(id)
dispatch(order);
}
)()
}, [dispatch, id])