gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:42:29 26 4
gpt4 key购买 nike

我有以下功能:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com