-2

Why this example is working

 showNext = () => {
    const { current, total } = this.state;
    this.setState({
      current: current + 1 === total ? 0 : current + 1
    });
  };

and this one is not

 showNext = () => {
    const { current, total } = this.state;
    this.setState({
      current: current++ === total ? 0 : current++
    });
  };

And this one isn't working too

  showNext = () => {
    const { current, total } = this.state;
    this.setState({
      current: ++current === total ? 0 : ++current
    });
  };
You Nguyen
  • 9,961
  • 4
  • 26
  • 52
Roman T
  • 113
  • 1
  • 9

1 Answers1

1

Because var +1 creates a new variable that is evaluated and leaves var untouched.

Post and pre increment (++var var++) actually change the variable. So in these examples you are increasing the variable both when you evaluate it and when you assign it.

Tu.Ma.
  • 1,325
  • 10
  • 27