In the following example I declare a variable myVar
with useState() hook. I am trying to check its value and according to this do some things. I know through the useEffect() hook that myVar
has the value it is supposed to have. Though when I try to check if it's equal with some value in if statements, those comparisons are clearly calculated wrong. Is there some workaround to ckeck myVar
variable's value? Thank you in advance!
const Example = () => {
const [myVar, setMyVar] = useState(0);
useEffect(() => {
// Consoles the correct value
console.log(myVar);
}, [myVar])
useEffect(() => {
(async () => {
if (!loading) {
await runAnimation();
}
})()
}, [loading])
const runAnimation = async () => {
setMyVar(myVar + 1);
someFunction();
}
const someFunction = async () => {
response = await controls.start();
// Gets in wrong statements
if (myVar === 0) {
doZero();
} else if (myVar === 1) {
doOne();
} else if (myVar === 2) {
setMyVar(myVar - 1);
doTwo();
}
}
return (
<div>
<SomeComponents />
</div>
)
}