【发布时间】:2017-07-15 17:14:03
【问题描述】:
我需要将数组中的第一个元素作为最后一个。
问题在于它是指向对象的链接。
如何使用不变性助手来做到这一点。
我现有的代码如下。
state = {
table: [['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
['', '', '', '']],
}
appendRow = () => {
let newTable = deepcopy(this.state.table);
newTable.push(this.state.table[0].slice())
this.setState({ table: newTable });
}
我对不变性助手的建议不好
appendRow = () => {
this.setState({ table: update(this.state.table, { $push: [this.state.table[0].slice()] }) });
}
因为我再次使用slice()。
如何做得更好?
【问题讨论】:
标签: javascript reactjs immutability immutable.js