2

我有存储数组(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)}

我需要将 action 对象添加到数组的索引 todos 中:我尝试过使用这个 reducer,但出现错误:

state.calendar[newTodo.day].concat 不是函数

我的减速机:

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
    });
}
4

3 回答 3

2

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

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

首先得到dayand todoItem,你可以使用解构:

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

然后复制您的日历,您可以使用扩展语法:

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

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

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

然后返回更新的状态:

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));

于 2019-03-21T11:28:03.010 回答
0

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

于 2019-03-20T20:42:14.693 回答
-1

你可以试试这个:

减速器:

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;

于 2019-03-21T11:08:19.033 回答