1

I am very new to react and material-IU. This is the structure that is passed to each selector to configure it and that shows the three default options

export const questionOptions = {
  defaultValue: undefined,
  control: {
    type: "select",
    options: {      
      Accept: true,
      Decline: false,
      Unknown: undefined,
    },
  },
};

And this is how each component is configured for the title and its options

export const types = {
  startAdornment: {
  title: "First Text",
  ...questionOptions,
  },
  startAdornment: {
  title: "Second Text",
  ...questionOptions,
  },
}

I have to create a third select and I want to show only the first two options so I clone the object and modify the properties I want

export const questionOptionsTwo = Object.assign({}, questionOptions);

questionOptionsTwo.control.options = { Accept: true, Decline: false, } 

And then use this new object for the third select

startAdornment: { 
   title: "Third Text", 
   ...questionOptionsTwo, 
}, 

the problem is that, although I am cloning the original object, when I modify the properties in the cloned object they are also modified in the original.

What am I doing wrong? thank you all in advance

homerThinking
  • 785
  • 3
  • 11
  • 28
  • 1
    because this is not a clone, this is te same object with different addressins names – Mister Jojo Dec 08 '20 at 17:57
  • 1
    you are doing what is called a shallow copy, objects are referenced in javascript. please see: https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript – iwaduarte Dec 08 '20 at 17:57
  • 2
    Does this answer your question? [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – iwaduarte Dec 08 '20 at 17:58
  • 2
    `Object.assign` is only a shallow copy, so `questionOptionsTwo.control` still refers to the exact same object as `questionOptions.control` – TKoL Dec 08 '20 at 17:58
  • 1
    deep copy vs shallow copy – Nonik Dec 08 '20 at 17:59

0 Answers0