1

For example this code:

export const Test = () => {
  const [counter, setCounter] = useState(0);
  let number;

  const DivideByCounter = () => {
     number = 10 / counter;
  }

  const IncreaseCounter = () => {
      for(let i = 0; i < 5; i++) {
         setCounter(counter + i);
  }
}

It's just an example of a code that I'm working on. The problem here is that the setCounter is updating the state correctly, however, when I use the counter to divide, it's returning me Infinity. When I console.log(counter) it's showing me 0, the initial state, however, outside the function, it shows me the updated state. Does everybody know how can I pass the update state into the function so it won't return me Infinity? Thanks in advance :)

2
  • How are you calling DivideByCounter, is it possible you are doing so before the state has finished updating? (As setting state is asynchronous) Commented Jul 6, 2021 at 14:26
  • use useCallback Commented Jul 6, 2021 at 14:27

3 Answers 3

1

Your problem could be connected to the fact that you call DivideByCounter before counter will be updated. Just add a check in DivideByCounter that, if counter === 0 then number = 0. Something like:

export const Test = () => {
  const [counter, setCounter] = useState(0);
  let number;

  const DivideByCounter = () => {
     if (counter === 0) number = 0;
     else number = 10 / counter;
  }

  const IncreaseCounter = () => {
      for(let i = 0; i < 5; i++) {
         setCounter(counter + i);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The check has worked, now I will try to add to useEffect
0

You're initial value is set to zero so when you divide for the first time you are dividing by zero which gives infinity

Maybe try adding an exception for zero like the other answer and add a useEffect containing the Divide by counter code and add counter as a dependency

1 Comment

I'm using the useEffect, however with no dependencies. The exception worked, now it's returning me the value that it's expected, I will try adding counter as a dependency
0

The Effect Hook lets you perform side effects in function components, in simple words, it means, it lets you run code when state changes or when a prop changes.

you can use UseEffect to update number only when there is a change to your counter, or a change to another prop that you want.

const [counter, setCounter] = useState(0);
  let number;

  useEffect(() => {
    DivideByCounter();
    console.log(number);
  }, [counter]);

  const DivideByCounter = () => {
    number = 10 / counter;
  };

  const IncreaseCounter = () => {
    for (let i = 0; i < 5; i++) {
      setCounter(counter + i);
    }
  };

you can share how did you try to call DivideByCounter?

1 Comment

I'm not 100% sure but in IncreaseCounter first for iteration sets counter to 0 and in that case DivideByCounter sets number to Infinity again... Right?

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.