为什么将其视为对NgRx中存储值的直接修改

问题描述 投票:0回答:1

请考虑以下语法,该语法有效:

有关摘要的信息:状态具有一个切片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;

发生错误。

但是我想我已经在第一行本身中复制了状态。我在这里想念的是什么?

angular ngrx ngrx-store
1个回答
0
投票

看起来不错,我看到的情况是当targetIndex为-1description未定义时,应返回原始的state。您还可以添加错误信息吗?

© www.soinside.com 2019 - 2024. All rights reserved.