0

There are 4 screens on a page which I called them viewPort. On button click I want to add an Ellipse of predefined settings on the selected viewPort.

viewPort(1/2/3/4) is an object that looks like this:

{      
  regionLines: [
    {
      uid: "region-line-1",
      startPoint: { x: 1476, y: 1097 },
      middlePoint: { x: 2149, y: 1601 },
      endPoint: { x: 2021, y: 2242 },
      
    },
    {
      uid: "region-line-2",
      startPoint: { x: 1528, y: 921 },
      middlePoint: { x: 2294, y: 1564 },
      endPoint: { x: 2230, y: 2279 },
    },
  ],
  lesions: [
    {
      uid: "lesion-1",
      type: "ellipse-lesion",
      point: { x: 1857, y: 896 },
      xRadius: 135,
      yRadius: 75,
      rotationAngle: 25,
      isEditable: true,
      isVisible: true,
    },
    {
      uid: "lesion-2",
      type: "polyline-lesion",
      isEditable: true,
      isVisible: true,
      points: [
        { x: 1624, y: 1026 },
        { x: 1722, y: 969 }
      ],
    },
  ],
};

The problem I am facing is with setViewPort(1/2/3/4). In if statement when the setViewPort is run it updates all other setViewPort as well.

function setFinding() {    
 const addEllipse = {
  uid: "lesion-32321",
  type: "ellipse-lesion",
  point: { x: 2857, y: 896 },
  xRadius: 135,
  yRadius: 75,
  rotationAngle: 25,
  isEditable: true,
  isVisible: true,
  style: {
   color: "#0E5715",
   fillColor: "#0E5715",
   fillOpacity: 0.2,
   dashedLine: true,
  },
}
let newLesion
if(viewportsCore.layoutController.getActiveContainerId()==='viewport-container-1-1'){
   newLesion = viewPort1
  newLesion.lesions.push(addEllipse)
  // console.log('this is new lesion info in viewport 1', newLesion)
  setViewPort1(newLesion)

} else if(viewportsCore.layoutController.getActiveContainerId()==='viewport-container-2-1'){
  // console.log('this is new lesion info in viewport2 before', viewPort1, viewPort2)
   newLesion = viewPort2
  // console.log('this is new lesion info in viewport2 after', newLesion)
  newLesion.lesions.push(addEllipse)
  setViewPort2(newLesion)

  
} else if(viewportsCore.layoutController.getActiveContainerId()==='viewport-container-1-2'){
   newLesion = viewPort3
  newLesion.lesions.push(addEllipse)
  // console.log('this is new lesion info in viewport 3', newLesion)
  setViewPort3(newLesion)

} else{
   newLesion = viewPort4
  newLesion.lesions.push(addEllipse)
  // console.log('this is new lesion info viewport4', newLesion)
  setViewPort4(newLesion)    
}
viewportsCore.mgLesionsController.createMGLesion(
  viewportsCore.layoutController.getActiveContainerId(),
  newLesion
);
}

Is there any way to stop updating all other setState values? Or am I missing anything?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Ragu
  • 155
  • 2
  • 12
  • `.push` mutates the current array. `newLesion = viewPort4` does not copy the array, both share the same array instance. – Emile Bergeron Nov 15 '21 at 19:42
  • Then how can I add a new object in 'lesions' array of objects? any suggestion! – Ragu Nov 15 '21 at 19:51
  • 1
    Although it is a redux doc it's immensely helpful in understanding how to immutably update a React state: [immutable update patterns](https://redux.js.org/usage/structuring-reducers/immutable-update-patterns) – Drew Reese Nov 15 '21 at 19:55
  • There's this other question that addresses updating nested state properties: [How to update nested state properties in React](https://stackoverflow.com/q/43040721/1218980) – Emile Bergeron Nov 15 '21 at 19:57

0 Answers0