I want to make a "hard copy" of the object that is assigned to this.state and change the copy in the sayHello method. When I use Object.assign it changes this.state. How can I fix this? Or......can I ? ~~~~~~
class App {
constructor() {
this.state = {
items: []
}
}
sayHello() {
//______I want to clone this.state so I can modify the copy____
//______I use Object.assign()__________________________________
let objCopy = Object.assign({}, this.state)
//_____I change the copy_______________________________________
objCopy.items.push({ text: "hi there" })
//_____But the original was changed...nooooooooo!______________
console.log(this.state); // Changed! { items: [ { text: 'hi there' } ] }
}
}
let myApp = new App()
myApp.sayHello();