简体   繁体   English

试图从嵌套的 redux object 中删除所有项目并添加传入项目

[英]trying to remove all items and add incoming items from a nested redux object

I have below reducer and below initial State.我有低于减速器和低于初始 State。 classinfo is the parent state which has a nested state array of students. classinfo 是父 state,它有一个嵌套的 state 学生数组。 with the below reducer I am planning (replace previous students with new students ) to delete all students from the previous state, add new students from "action.data.students" and return a new state.使用以下减速器,我计划(用新学生替换以前的学生)从以前的 state 中删除所有学生,从“action.data.students”添加新学生并返回一个新的 state。 first time I add students there is no issue, when I add another student I am getting the error "A state mutation was detected between dispatches" please let me know, where I am doing it wrong.第一次添加学生时没有问题,当我添加另一个学生时,我收到错误“在调度之间检测到 state 突变”请告诉我,我做错了什么。

classInfo[ { Id:"", students:[] }]

function sampleReducer(state = initialState.classInfo, action) {
  switch (action.type) {
    case types.ADD_CLASSROOMS:
      return [...state, ...action.data];
    case types.REMOVE_CLASSROOMS:
      return state.filter((class) => class.id !== action.data);
    case types.ADD_STUDENT_DETAILS:
      const stateObj = state.map((class, i) => {
        if (class.id === action.data.id) {
          return {
            ...class,
            students: {
              ...action.data.students,
            },
          };
        }
        return {
          ...class,
        };


      });
      return stateObj;

    default:
      return state;
  }
}

You are spreading object for students .您正在为students传播 object 。 It's an array.这是一个数组。 So use square brackets and spread the students array - students: [...action.data.students]所以使用方括号并传播学生数组 - students: [...action.data.students]

...
case types.ADD_STUDENT_DETAILS:
      const stateObj = state.map((class, i) => {
        if (class.id === action.data.id) {
          return {
            ...class,
            students: [ //<----use square brackets(as its an array)
              ...action.data.students
            ],
          };
        }
        return class;


      });
      return stateObj;
    ...

You are doing great, do not to mutate the state simply means, do not alter the prevState just update the state.你做得很好, do not to mutate the state只是意味着不要改变prevState只需更新 state。

The main error is that, you are trying to change state of student as previously it was array type and while you are updating it you made it as object type just a typo.主要错误是,您正在尝试更改学生的 state,因为它以前是array类型,而在更新它时,您将其设置为object类型只是一个错字。 please use [ ] instead of { }请使用 [ ] 而不是 { }

 const state = { id: 1, students: [ {first: 1}, {second: 2}, {third: 3} ] } const action = { data: { students: [ {fourth: 4} ] } } const updatedStudents = {...action.data.students } console.log(state); console.log(updatedStudents);

So, in your case->所以,在你的情况下->

case types.ADD_STUDENT_DETAILS:
      const stateObj = state.map((class, i) => {
        if (class.id === action.data.id) {
          return {
            ...class,
            students: [
              ...action.data.students,
            ],
          };
        }
        return {
          ...class,
        };


      });
      return stateObj;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM