javascript - 当你通过引用传递时,我们会改变状态吗

标签 javascript reactjs

我有以下功能:

filterList(event) {
  var updateList = this.state.initialItems;

  updateList = updateList.filter(function(item) {
                  return item.search(event.target.value) !== -1;
               });

  this.setState({currentItems: updateList });
}

如果我们通过引用传递,updateList 和 this.state.initialItems 不是指向同一件事吗?

如何复制 this.state.initialItems?

最佳答案

If we are passing by reference isn't updateList and this.state.initialItems pointing to the same thing?

没有。您将返回一个全新的数组(根据 MDN ):

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

关于javascript - 当你通过引用传递时,我们会改变状态吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47620651/

相关文章:

javascript - 如何使用 Javascript 函数在下拉选择值上启用或禁用 asp 按钮?

javascript - 强制 Backbone 或 Underscore 总是转义所有变量?

reactjs - 无法使用导入文件中的类

css - 我在 react-datepicker 位置上遇到问题

javascript - 合并包含数组的对象

javascript - 简单小巧的纯 javascript 灯箱(对话框覆盖)?

javascript - 无法在 React Web App 中导入 css 文件

reactjs - Enzyme,如何通过属性找到wrapper的 child ?

reactjs - 在开发模式下使用create-react-app时如何将应用程序放入子文件夹中?

javascript - jQuery tablesorter - 对包含混合文本和数字的列进行排序