【发布时间】:2022-01-23 01:44:43
【问题描述】:
我有一个包含对象数组的 redux 状态,对于每个对象,我调用一个 api 来获取更多数据
objects.forEach((obj, index) => {
let newObj = { ...obj };
service.getMoreData()
.then(result => {
newObj.data = result;
let newObjects = [...this.props.objectsList] ;
let index = newObjects.findIndex(el => el.id === newObj.id);
if (index != -1) {
newObjects[index] = newObj;
this.props.updateMyState({ objectsList: newObjects });
}
})
当我得到两个非常接近的响应时,状态没有正确更新,我丢失了第一个响应的数据。
更新数组的单个元素的正确方法是什么?谢谢!
【问题讨论】: