define an array
this.state = {
list: [],
obj: {}
}
const list = [
{id: 1},
{id: 2}
]
this.setState({
list
})
take out an object
const obj = list[0]
this.setState({
obj
})
modify the object, modify the value, and then setState
obj.id = 3
this.setState({
obj
})
here comes the problem. May I ask the reason?
- if you modify it like this, the obj value in list will also change, and it will look like this
[
{id: 3},
{id: 2}
]
- even if obj and list are cut off, changing the value of obj,list will still change
if you do the following
const list1 = [].concat(list)
const obj = list1[0]
// objlistobj