TypeError: counters.indexOf is not a function in React list
TypeError: counters.indexOf is not a function in React list
我有一个处于 class 状态的列表,当调用单击事件时,我需要从列表中获取选定的组件。这是我的代码
class Counters extends Component {
state = {
counters: [
{ id: 1, value: 0 },
{ id: 2, value: 2 },
{ id: 3, value: 2 },
{ id: 4, value: 3 }
]
};
handleIncrement = counter => {
const counters = { ...this.state.counters };
console.log(counter); //this returns when this triggerrd {id: 2, value: 2}
const index = counters.indexOf(counter);
};
您正在将数组的值复制到对象中。
const counters = [...this.state.counters]
我有一个处于 class 状态的列表,当调用单击事件时,我需要从列表中获取选定的组件。这是我的代码
class Counters extends Component {
state = {
counters: [
{ id: 1, value: 0 },
{ id: 2, value: 2 },
{ id: 3, value: 2 },
{ id: 4, value: 3 }
]
};
handleIncrement = counter => {
const counters = { ...this.state.counters };
console.log(counter); //this returns when this triggerrd {id: 2, value: 2}
const index = counters.indexOf(counter);
};
您正在将数组的值复制到对象中。
const counters = [...this.state.counters]