如何在reducer中的数组索引处添加对象

How to add object at array index in reducer

我有数组存储 (store.calendar[0].todos[{title: title, etc...}])

    0: {todos: Array(0)}
    1: {todos: Array(0)}
    2: {todos: Array(0)}
    3: {todos: Array(0)}
    4: {todos: Array(0)}

我需要将操作对象添加到数组的待办事项索引中: 我试过这个减速器,但我得到一个错误:

state.calendar[newTodo.day].concat is not a function

我的减速器:

let initialState = { calendar: []}

for (let i = 1; i <= 30; i++) {
  initialState.calendar.push({ todos: []});
}

const todosReducer = (state = initialState, action) => {
  switch (action.type) {
    case ADD_TODO:
      const newTodo = action.todoItem;
      const newStore = {...state,
            todos: state.calendar[newTodo.day].concat(newTodo)};
      return newStore;
    default:
      return state
  }
}

export default todosReducer;

我的操作:

export const addTodoAction = (todoItem) => {
  return {
    type: ADD_TODO,
    todoItem
  }
}

我的添加待办事项功能:

const handleSaveTodo = () => {
    props.addTodo({ 
      day: 5,
      title: trackInput.value,
      description: trackTextarea.value,
      completed: false
    });
}

试试 state.calendar[newTodo.day].todo.concat(newTodo)。我认为您正在尝试 .concat() 到对象 {todo: Array(0)} 而不是其中的数组。

你可以试试这个:

减速器:

const initialState = { 
  calendar: Array.from({length: 30}, () => ({ todos: [] }))
}

const todosReducer = (state = initialState, action) => {
  switch (action.type) {
    case ADD_TODO:
      const { todoItem } = action;
      const newCalendar = [...state.calendar];
      newCalendar[todoItem].todos.push(todoItem);
      
      return {
        ...state,
        calendar: newCalendar
      }
    default:
      return state
  }
}

export default todosReducer;

你需要以一种完全不可变的方式改变你的状态。

为此,您必须复制日历数组、在此日历中更新的日期以及要追加到的待办事项列表。

首先得到daytodoItem,可以使用解构:

const { todoItem } = action;
const { day } = todoItem;

然后复制你的日历,你可以使用传播语法:

const calendar = [...state.calendar];

然后用当天的副本更新相关日期,并将新的待办事项附加到待办事项列表中:

calendar[day] = { ...calendar[day], todos: [...calendar[day].todos, todoItem] };

然后return更新状态:

return { ...state, calendar };

这是一个例子:

const ADD_TODO = 'add-todo';

const initialState = { calendar: Array.from({ length: 30 }, (_, i) => ({ todos: [] })) };

const todosReducer = (state = initialState, action) => {
  switch (action.type) {
    case ADD_TODO:
      const { todoItem } = action;
      const { day } = todoItem;
      const calendar = [...state.calendar];
      calendar[day] = { ...calendar[day], todos: [...calendar[day].todos, todoItem] };
      return { ...state, calendar };
    default:
      return state
  }
}

let state = initialState;

state = todosReducer(state, { type: ADD_TODO, todoItem: { day: 0, title: 'todo day 1' } });
state = todosReducer(state, { type: ADD_TODO, todoItem: { day: 1, title: 'todo day 2' } });
state = todosReducer(state, { type: ADD_TODO, todoItem: { day: 2, title: 'todo day 3' } });
state = todosReducer(state, { type: ADD_TODO, todoItem: { day: 2, title: 'second todo day 3' } });

console.log(state.calendar.slice(0, 3));