Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
React - How to check equality for state variable declared with useState()

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>
    )

}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!