请考虑以下语法,该语法有效:
有关摘要的信息:状态具有一个切片educationList
,该切片是各种教育(BS,MS,PhD)的数组。 educationList
数组中的每个项目都包含另一个数组description
,其中包含项目,论文等的信息。
以下是删除description
数组之一的代码段:
case Actions.DELETE_ROLE: {
let updatedEducationList = [...state.educationList];
const targetIndex = updatedEducationList.findIndex((educationItem) => {
return educationItem.id === action.payload.educationId;
});
let oldDescription = updatedEducationList[targetIndex].description;
let newDescription = oldDescription.filter((item, index) => index !== action.payload.roleIndex);
updatedEducationList[targetIndex] = { ...updatedEducationList[targetIndex], description: newDescription };
return { ...state, educationList: updatedEducationList };
}
如果代码段中的以下行,
updatedEducationList[targetIndex] = { ...updatedEducationList[targetIndex], description: newDescription }
被替换为
updatedEducationList[targetIndex] = newDescription;
发生错误。
但是我想我已经在第一行本身中复制了状态。我在这里想念的是什么?
看起来不错,我看到的情况是当targetIndex为-1
或description
未定义时,应返回原始的state
。您还可以添加错误信息吗?