React Reducer - 将元素添加到现有元素

React Reducer - Add elements to existing element

我希望新的更新被连续添加到“看板”,而不是旧值被签署。 enter image description here

动作函数

  export const editExpense = (id, updates) => ({
  type: "EDIT_EXPENSE",
  id,
  updates
});

减速器功能

case "EDIT_EXPENSE":
  return state.map((expense) => {
    if (expense.id === action.id) {
      return {
        ...expense,
        kanbanboard:[{...action.updates}]
      };
    } else {
      return expense;
    };
  });

感谢您的帮助。

我看不到整个减速器,但看起来您正在明确覆盖旧值。将其更改为:

case "EDIT_EXPENSE":
  return state.map((expense) => {
    if (expense.id === action.id) {
      return {
        ...expense,
        kanbanboard:[
         ...expense.kanbanboard, // you have to add your old state here
         { 
           ...action.updates
        }]
      };
    } else {
      return expense;
    };
  });