I am reading the document of Reactjs about not mutating data. I do not understand the difference between 2 pieces of code in document's example:
handleClick() {
// This section is bad style and causes a bug
const words = this.state.words;
words.push('marklar');
this.setState({words: words});
}
and:
handleClick() {
this.setState(prevState => ({
words: prevState.words.concat(['marklar'])
}));
}
Why the second code does not mutate the data?
concatand does not mutate current stateconcatreturns a new array.