我在这里修改我的 Redux 状态吗?
Am I modifying my Redux state here?
我不确定这是否在修改我的 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 不会修改原始数组并在没有参数的情况下调用它returns原始数组的副本。
在此之后修改tempArray
将不会影响this.props.currentView.someArray
我不确定这是否在修改我的 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 不会修改原始数组并在没有参数的情况下调用它returns原始数组的副本。
在此之后修改tempArray
将不会影响this.props.currentView.someArray