Home > Back-end >  React updating single object of an array of objects - map vs entire array
React updating single object of an array of objects - map vs entire array

Time:05-17

I have a component with an array of objects in local state:

    const [myState, setState] = useState<SomeObjectType[]>([]);

I can update a single object in that array by making a copy of the entire array and then update the property of the single object I wish to update:

OPTION 1


    const state = [...myState];
    state[id].description = description;
    setState(state);

Or I can use map:

OPTION 2


  const newState = talkingPoints.map(el => {
      //            
  • Related