【问题标题】:React Reducer - Add elements to existing elementReact Reducer - 将元素添加到现有元素
【发布时间】:2022-01-06 17:07:44
【问题描述】:

我希望新的更新可以持续添加到“看板”中,但旧值会被签名。 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;
    };
  });

感谢您的帮助。

【问题讨论】:

    标签: reactjs react-native action reducers


    【解决方案1】:

    我看不到整个 reducer,但看起来您明确地覆盖了旧值。将其更改为:

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

    【讨论】:

    • 不错。有用。谢谢
    • 很高兴听到!你能把问题标记为已回答吗?
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多