Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda