In Lecture 10-Removing the Local State of the ‘Composing Components’ section of Mastering React, Mosh refactors his code to move the function definition of handleIncrement() from the Counter component into the Counters component. The newly-created method in Counters follows. I starred the line of code in this method that is bothering me below:
handleIncrement = counter => {
const counters = [...this.state.counters];
const index = counters.indexOf(counter);
counters[index] = {...counter}; ***
counters[index].value++;
this.setState({counters});
}
***counters[index] = {...counter};
This is where I’m confused. It seems that all this is doing is spreading the contents of the raised counter object and assigning them to the index of the array that it already occupied. Since the attributes of the counter object aren’t being modified by the Counter component, shouldn’t we be able to assume that the value of counters[index] is already equal to {…counter}? Furthermore, I understand the idea that we don’t want to modify the state directly, but since we are working with a cloned array and not the state already, I don’t see how this line of code is accomplishing anything along those lines. In other words, what purpose is this line of code actually serving? I was able to comment it out and run the app with apparently full functionality, so I’m wondering if this line is useful in more complex applications for some reason that I am not grasping.