【发布时间】:2017-09-11 02:25:33
【问题描述】:
我不确定这是否正在修改我的 redux 状态:
var tempArray = this.props.currentView.someArray;
tempArray.push(this.state.inputField);
第一行是复制内容,还是创建了对 props 对象的实际引用?
【问题讨论】:
标签: javascript reactjs redux flux reactjs-flux
我不确定这是否正在修改我的 redux 状态:
var tempArray = this.props.currentView.someArray;
tempArray.push(this.state.inputField);
第一行是复制内容,还是创建了对 props 对象的实际引用?
【问题讨论】:
标签: javascript reactjs redux flux reactjs-flux
var tempArray = this.props.currentView.someArray;
将使 tempArray 引用数组。
tempArray.push() 修改引用。
所以是的,它会修改this.props.currentView.someArray。
如果你不想修改你的状态,你可以这样做。
var tempArray = this.props.currentView.someArray.slice();
Slice 不会修改原始数组,并且不带参数调用它会返回原始数组的副本。
在此之后修改tempArray不会对this.props.currentView.someArray产生影响
【讨论】: