Tengo un problema con redux. Aquí está mi código:
acciones.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 }; }algúnComponente.ts
useEffect(() => { dispatch(getOrders(id)); //here is problem }, [dispatch, id]) ¿Alguien puede decirme por qué no puedo usar getIncidents(id) dentro del dispatch ? Cuando pongo esta función, VSCode me arroja este error:
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)no se como solucionar esto :/
¡Gracias por cualquier ayuda!
Intente esperar a que se resuelva la promise , luego dispatch sus acciones:
async/await :
useEffect(() => { ( async function handleAsync() { let order = await getOrders(id) dispatch(order); } )() }, [dispatch, id])