Home > other >  Replace/update data in Switch case
Replace/update data in Switch case

Time:11-26

The action data contains an array, I want, according to the id, to replace a specific array with new data. But it turns out that instead of updating one array, I get 3 array elements that contain other arrays.

Other records that do not match by id should remain the same

 case UpdateOneBank:
      return {
        ...state,
        data: state.data.map((p) => p._id === action.data._id?action.data:state.data),}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

data state old data state

New data New data Update only one array, not to add 3 in to 2 remain arrays enter image description here

CodePudding user response:

case UpdateOneBank:
  return {
    ...state,
    data: state.data.map((p) => p._id === action.data._id?action.data:p),}

Try this

You were returning the entire data in else condition, not an item of map.

CodePudding user response:


Solution

   case UpdateOneBank:
        return {
           ...state,
           data: state.data.map((originalValue) => originalValue._id === action.data._id?  action.data:  originalValue),}

Error

Instead of doing this:

case UpdateOneBank:
      return {
        ...state,
        data: state.data.map((originalValue) => originalValue._id === action.data._id?  action.data:  state.data),}

Reason

Note: Everytime you are entering in else condition, you are pushing/returning your own state.data array

  • Related