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 :)
DivideByCounter, is it possible you are doing so before the state has finished updating? (As setting state is asynchronous)