1

I'm a React learner and I want to understand something. The code below doesn't work, it's just a component with two buttons that add or substract a number and display it. Here's the whole code :

import React from "react";
import { useState } from "react";

const Compteur = () => {
  let i = 0;
  const [number, setNumber] = useState(0);

  const increment = () => {
    i++;
    setNumber(i);
  };

  const decrement = () => {
    i--;
    setNumber(i);
  };

  return (
    <div>
      <button type="button" onClick={increment}>
        +
      </button>
      <button type="button" onClick={decrement}>
        -
      </button>
      <p>Number : {number}</p>
    </div>
  );
};

export default Compteur;

But if I replace setNumber(i) by setNumber(number + 1), it works well. What's the problem with this i variable ? Thanks for your help !

1
  • 2
    The scope of i and the functions that capture it are remade every render. It needs to also use useState. Commented Nov 10, 2021 at 21:44

3 Answers 3

1

Welcome to React!

The problem in your example is that each time the component rerenders (in your case due to a state change), i is reinitialized and assigned a value of 0. I think you're thinking of the component more as a loop, versus a javascript file. React does a great job as maintaining state, and that state will stay the same though rerenders of the component, which is why it's working as expected when you use number (a piece of React state) instead of i

Sign up to request clarification or add additional context in comments.

2 Comments

also, side note, instead of setNumber(number +1), you'll want to do setNumber((prevState) => prevState + 1) since updates to state are asynchronous and the first way can lead to some unexpected behavior.
@DaveVanFleet In this case I think you're actually going to be fine, because you're not relying on it to update another piece of state. You're right that you can't rely on these to be synchronous, but because of how it's written it doesn't need to be, setNumber(number + 1) should work just fine.
1

So your problem here is setting let i = 0; as react code is rerun on every rerender, which is caused by state change (among other ways).

So when you call setNumber react then says "okay, rerender the page" and it makes its way down your code, and sees "set i to be 0" and then when you increment the code is saying "i++" or in this case "0 + 1" (because i is set to 0 again).

Essentially, every time react rerenders the component, it's going to reset the value in the variable. That's why we have to use useState because react is then able to keep track of those values.

Your example setNumber(number + 1) is how you should be writing this.

1 Comment

Thank you ! I better understand how it works !
1

You are using the variable i which is getting reinitalized every render, your code should be something like this

const increment = () => {
    setNumber(number + 1);
}

const decrement = () => {
    setNumber(number - 1);
}

2 Comments

This is not quite right, as number++ will attempt to change the value of number which is not valid for react state updates. You instead would have to write setNumber(number + 1).
Oh, thanks. Forgot that ++ and -- are also assignment operators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.