0

i want to change the existing item with following function, but instead of overwriting a new item will be created. Whats my mistake ?

  case "EDIT_KSTORY":
  return state.map((storie) => {
    if (
      storie.stories.storieID === action.sID &&
      storie.aNoteId === action.aNid
    ) {
      return {
        ...storie,

        stories: [
          ...storie.stories,
          {
            ...action.updates,
          },
        ],
      };
    } else {
      return storie;
    }
  });
Maxxxus
  • 3
  • 2

1 Answers1

0

try not to mutate all states, and mutate only in places where a data should be changed. I don't know all structure, move but might be:

case "EDIT_KSTORY": 
return {
    ...state,
    stories: state.stories.map((storie) => {
        if (
          storie.stories.storieID === action.sID &&
          storie.aNoteId === action.aNid
        ) {
          return {
            ...storie,
    
            stories: [
              ...storie.stories,
              {
                ...action.updates,
              },
            ],
          };
        } else {
          return storie;
        }
      });
}
Anna Vlasenko
  • 916
  • 5
  • 8