0
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?

MatasLiu
  • 49
  • 6
  • `isLooping` is a constant, it doesn't change its value in the middle of a function. See [useState set method not reflecting change immediately](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) – Guy Incognito Oct 07 '20 at 16:30
  • `// this always prints true` this is expected because you have this line in a `while(isLooping)` – Yury Tarabanko Oct 07 '20 at 16:31

0 Answers0