定義一個(gè)數(shù)組this.state = { list: [], obj: {}
}const list = [
{id: 1},
{id: 2}
]this.setState({ list})取出一個(gè)對(duì)象const obj = list[0]this.setState({
obj
})修改對(duì)象,修改值,然后setStateobj.id = 3this.setState({
obj
})問(wèn)題來(lái)了,請(qǐng)問(wèn)原因?像這樣修改,list中obj值也會(huì)發(fā)生變化,會(huì)成這樣[
{id: 3},
{id: 2}
]即使把obj和list切斷,如下操作,修改obj,list的值依然會(huì)變化const list1 = [].concat(list)const obj = list1[0]// 然后修改obj,list中obj的值依然會(huì)變
添加回答
舉報(bào)
0/150
提交
取消