Https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-react-part-ii/modules/fecp-advanced-react/lessons/container-presentational-components/exercises/container-presentational-components-intro

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-react-part-ii/modules/fecp-advanced-react/lessons/container-presentational-components/exercises/container-presentational-components-intro
i’m confusing in this chapter i try to understand the code so the engineer inside the code right a method "nextGP " like this :

  nextGP() {
    let current = this.state.currentGP;
    let next = ++current % GUINEAPATHS.length;
    this.setState({ currentGP: next });
  }

the confuse is in the second expression he increment the current like this “++current” we can increment variable like this in react ?
and thank you for your help in advance

Hi, that’s nothing specific to React. It’s common incrementation as it is done in Javascript. MDN docs:

If used postfix, with operator after operand (for example, x++ ), the increment operator increments and returns the value before incrementing.

If used prefix, with operator before operand (for example, ++x ), the increment operator increments and returns the value after incrementing.

1 Like

you are alawys on time.thanks a lot my friend. but how the increment operator increments and returns the value before the incrementing because return value after the increments it’s logic.

Try it:

let counter = 0;
let prefixCounter = ++counter;
console.log(counter, prefixCounter); // 1, 1

counter = 0;
let postfixCounter = counter++;
console.log(counter, postfixCounter); // 1, 0
1 Like

nice example for understanding.thanks friend.so the operand before is more fast than the operand after.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.