const [isLooping, setIsLooping] = useState(false);
useEffect(() => {
const startLoop = async () => { // async method
while(isLooping) {
console.log(isLooping); // this always prints true
waitOneSecond();
}
}
if(isLooping) {
startLoop();
}
console.log(isUpdating); // this correctly prints true or false
}, [isLooping]);
The problem is that the state hook isLooping
does not reflect real value when used from the async method. Why and how to fix this?