javascript - 我在这里修改我的 Redux 状态吗?

标签 javascript reactjs redux flux reactjs-flux

我不确定这是否会修改我的 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/

相关文章:

java - angularjs spring Rest文件上传表单

javascript - 对象数组是要存储在数据库还是js文件中?

javascript - 无法读取未定义的属性 'bind'

javascript - Redux 中间件在 next() 之前改变状态

javascript - 从数组中删除项目(React-Native + Redux)

javascript - jQuery 解除绑定(bind)和绑定(bind)

javascript - 如何禁用 webpack 4 代码拆分?

javascript - 加载到另一个 url 后,我如何使用 reactjs 中的路由从一个页面重定向

reactjs - React Router v6 - 访问嵌套路由和处理手写 url 的正确方法

javascript - 子组件应该在什么级别拥有自己的容器(逻辑react-redux的connect)组件才能管理自己的mapDispatchToProps?