【发布时间】:2020-07-31 19:23:33
【问题描述】:
我有一个调用动作的 index.js。一旦它调用一个动作,我想在该动作中触发多个调度动作。 Index.js 调用 handLoadCustomers 将调度 loadCustomers 函数,该函数调用 API 并调度另一个函数以将客户存储在状态中。
完成后,调用会返回到我想从一开始就使用客户的 handleLoadCustomers 调用,然后向那些客户发送handleExtraCustomerLoads的另一个动作,这将 调用另一个函数/动作。我如何在 React Redux 中做到这一点?
export function handleLoadCustomers() {
return function (dispatch) {
dispatch(loadCustomers())
.then((customers) => {
dispatch(handleExtraCustomerLoads(customers));
})
.catch((error) => {
throw error;
})
.then((newCustomers) => {
dispatch(handlePageLoadSuccess(newCustomers));
});
};
export function loadCustomers() {
return function (dispatch) {
return getCustomers()
.then((customers) => {
dispatch(loadCustomerSuccess(customers));
})
.catch((error) => {
throw error;
});
};
}
loadCustomers 后的customers 为空,根本不调度handleExtraCustomerLoads 函数
【问题讨论】:
-
是的,那么实际上什么不起作用?
-
@k-wasilewski customers 后 loadCustomers 为空且根本不调度 handleExtraCustomerLoads 函数
-
不返回
getCustomers,而是发送它 -
dispatch(loadCustomers())不是保证为什么这不起作用。
标签: reactjs redux react-redux