我试图了解这段代码是如何工作的,我正在使用 Array.prototype.indexOf() 来获取状态数组的索引,以便可以对其进行更新。我被告知在 indexOf() 的参数中传递一个对象,但我认为没有两个对象是相等的,那么 indexOf() 如何返回正确的索引我在这里遗漏了什么?这是我的代码 handleIncrement(counter){//passing object here from child component const counters = [...this.state.counters];//cloning const index = counters.indexOf(counter)//geting index of param counter console.log(counter === index)//how did index return true if this returns false??? counters[index] = {...counter};// spread object onto index pos counters[index].value++;//increment value this.setState({counters})//updates state just fine no errors }
添加回答
举报
0/150
提交
取消