【问题标题】:How to add object at array index in reducer如何在reducer的数组索引处添加对象
【发布时间】:2019-03-20 20:36:20
【问题描述】:

我有存储数组 (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 中: 我试过用这个减速器,但我得到一个错误:

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

【问题讨论】:

    标签: javascript arrays redux react-redux redux-store


    【解决方案1】:

    您需要以完全不可变的方式更改您的状态。

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        你可以试试这个:

        减速机:

        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;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-12
          • 1970-01-01
          • 2011-07-05
          • 2019-10-02
          • 2020-07-27
          相关资源
          最近更新 更多