我不确定这是否会修改我的 redux 状态:
var tempArray = this.props.currentView.someArray;
tempArray.push(this.state.inputField);
第一行是复制内容,还是创建对 props 对象的实际引用?
最佳答案
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
产生任何影响
关于javascript - 我在这里修改我的 Redux 状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43418103/