I have below reducer and below initial State. classinfo is the parent state which has a nested state array of students. 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. 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.
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;
}
}