我正在使用Redux更新我的不可变状态。我想通过简单地定位list []来更新我的reducer中的嵌套对象数组,因为我需要使用新对象来更新它。当我调度动作时,我的第一个项目的(board1)列表确实得到更新,但是一旦我为下一个或多个board2及以上的项目调度,它们将覆盖我的状态,并且返回单个项目。非常感谢您的帮助。.谢谢
const initialState = {
board: [
{ boardId: 1, boardname: "board1", list: [] },
{ boardId: 2, boardname: "board2", list: [] }
]
};
export default function(state = initialState, action) {
switch (action.type) {
case "ADD_LIST":
state = {
...state.board,
board: [
...state.board[action.payload.boardId - 1],
{
...state.board[action.payload.boardId - 1],
list: [
...state.board[action.payload.boardId - 1].list,
{
listId: state.board[action.payload.boardId - 1].list.length + 1,
listname: action.payload.listname
}
]
}
]
};
break;
default:
break;
}
return state;
}