JavaScript React Basics (retired) Designing Data Flow Communicating Events

見綸 陳
見綸 陳
6,333 Points

Do 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?