
見綸 陳
6,333 PointsDo not mutate state directly?
I use create-react-app to learn this course.
In following code. Changing argument 'index' to 'playerIndex' to help me understand.
onScoreChange: function(playerIndex, delta) {
console.log('onScoreChange', playerIndex, delta);
this.state.players[playerIndex].score += delta;
this.setState(this.state);
},
There is an problem showed in Chrome dev console:warning Do not mutate state directly. Use setState() react/no-direct-mutation-state.
Or check screenshot
Why does this problem happen?
Jakub Kašpar
16,383 PointsJakub Kašpar
16,383 PointsHere is your answer: https://teamtreehouse.com/community/is-it-better-to-update-a-specific-property-using-setstate-or-dump-the-whole-state-into-setstate