In the following code, I am not understanding why we need the following line, if counters is already equal to the state. What is the doing? Can I delete the line?
counters[index] = { ...counters[index] };
here is the whole thing.
class App extends Component {
state = {
counters: [
{ id: 1, value: 0 },
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 0 }
]
};
handleIncrement = counter => {
const counters = [...this.state.counters];
const index = counters.indexOf(counter);
counters[index] = { ...counters[index] };
counters[index].value++;
this.setState({ counters });
};