我刚刚改变了这个
const newCities = {...newState.cities};
newCities[action.payload.id] = action.payload;
newState.cities = newCities;
到单行
newState.cities = {...newState.cities, [action.payload.id]: action.payload};
但我该如何更改
const newCities = {...newState.cities};
action.payload.forEach(city=> newCities[city.id] = city);
newState.cities = newCities;
到单线?东西〜=
newState.cities = {...newState.cities, action.payload.map(city=> [city.id]: city)};
答案 0 :(得分:1)
最终解决方案:
News.category(params['categori']).tag(params['tag_list'])
以前我忘了初始化reduce array =)
旧方法: 可能是这样吗?
newState.cities = {...newState.cities, ...action.payload.reduce((o,a)=> ({...o,[a.id]:a}),{})};
<强>更新强>
newState.cities = {...newState.cities, ...action.payload.map(city=> ({[city.id]: city}) )};
更新(完整丑陋):
newState.cities = {...newState.cities, ...action.payload.reduce(city=> ({[city.id]:city}) )};