Reactjs 调用Axios,然后为每个返回的项目调用单独的Axios调用
我收到了要更新的Axios调用,但请求已返回到我的reducer。问题是,第一个axios调用更新我的状态,然后我的第二组承诺不会立即更新我的组件Reactjs 调用Axios,然后为每个返回的项目调用单独的Axios调用,reactjs,redux,Reactjs,Redux,我收到了要更新的Axios调用,但请求已返回到我的reducer。问题是,第一个axios调用更新我的状态,然后我的第二组承诺不会立即更新我的组件 export function selectRoutingBooks(routing) { const url = `${ROOT_ROUTINGS_URL}/${routing.RoutingId}/${routing.VersionId}/books`; let promises = []; const request =
export function selectRoutingBooks(routing) {
const url = `${ROOT_ROUTINGS_URL}/${routing.RoutingId}/${routing.VersionId}/books`;
let promises = [];
const request = axios.get(url).then((response)=>{
response.data.map((item)=>{
const url2 = `${ROOT_ITEMS_URL}/${item.MatItemKey}?all=true`;
promises.push(axios.get(url2));
});
axios.all(promises).then(function(results) {
results.forEach(function(res) {
let common = response.data.find(x=>x.MatItemKey === res.data.base.ItemKey);
let newCommon = {...common, ...res.data};
var i = response.data.findIndex(o => o.MatItemKey === newCommon.base.ItemKey);
if (response.data[i]) { response.data[i] = newCommon }
})
});
return response
});
return {
type: ROUTING_BOOKS,
payload: request
}
}
我不完全确定如何让我所有的承诺同时更新我的状态。当我登记入住时
componentWillUpdate(){
console.log("Component will update RoutingDetail", this.props.routingBooks)
}
我正在获取所有数据,但道具在这里不会更新
TIA我认为您应该使用Promise.all来完成您想做的事情,或者如果您正在使用ES7,则可以使用async
我找到了解决问题的办法。我需要把承诺还给你,都还给减速机
return Promise.all(promises).then(function(results) {
而不是
Promise.all(promises).then(function(results) {
这将在承诺实现后更新我的减速器 Promise.all的工作原理与axios.all相同。我将签入异步等待。