I am new to ReactJS and I found a curious thing about Object.assign()
:
const B = {
k1: 'b',
k2: 'bb',
treedata: [{
children: ['g']
}]
}
var A = Object.assign({}, B);
A.treedata[0].children = [1];
console.log(B)
As you can see, after Object.assign()
, changing object A
will also change object B
. Why does this happen and how can you avoid this?