我有低于减速器和低于初始状态。 classinfo是具有学生嵌套状态数组的父状态。我计划使用下面的化简器(用新学生替换以前的学生),以删除以前状态下的所有学生,从“ action.data.students”中添加新学生,然后返回新状态。第一次添加学生没有问题,当我添加另一个学生时出现错误 “在调度之间检测到状态突变” 请让我知道,我在哪里做错了。
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 doing great,
do not to mutate the state
simply means, do not alter theprevState
just update the state.使用此->
You are spreading object for
students
. It's an array. So use square brackets and spread the students array -students: [...action.data.students]