Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda