Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Reducer – Add elements to existing element

i want that new updates get continuous added to "kanbanboard" but instead the old value get signed over. enter image description here

ACTION-Function

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

REDUCER-FUNCTION

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Thank you for helping.

>Solution :

I cant see the entire reducer but it looks like you are explicitly overriding the old value. Change it to:

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;
    };
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading