I want my aysnc function
to react to state change so that it can terminate upon state change. What I have discovered is that it doesn't react to changing the state.
In the provided example I am changing the state from inside of the function, however I have tested it and changed the state from outside of function (Button click) and it doesn't work either.
const [testVariable, setTestVariable] = useState(false);
useEffect(()=> {
testFunction();
}, [])
const testFunction = async () => {
await timeout(1000);
console.log("Setting test variable to: " + true);
setTestVariable(true);
await timeout(1000);
console.log("Test variable is: " + testVariable);
}
Expected result is to see that the variable changed to true, however what I see is:
See Question&Answers more detail:os