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

154
Visualizações
How to run useState asynchronously
const Navbar = () => {

    const [isModalOpen, setisModalOpen] = useState(true)
    const [isLogin, setisLogin] = useState(true)

    const onClose = () =>{
        setisModalOpen(false)
        if(!isLogin){
            setisLogin(true)
        }
    }
    return  (<Modal isOpen={isModalOpen} onClose={() => onClose()} >
    {!isLogin ? 
        <Register />
        :
        <Login setisLogin={() => setisLogin(false)} />
    }
    </Modal>)
}

when I call onclose Function component gets re-renders once with the value of login then the modal is closing

Using setTimeout is solving my problem but I think using setTimeout is not a good practice.

 const Navbar = () => {

    const [isModalOpen, setisModalOpen] = useState(true)
    const [isLogin, setisLogin] = useState(true)

    const onClose = () =>{
        setisModalOpen(false)
        setTimeout(() => {
            if(!isLogin){
                setisLogin(true)
            }
        }, 300);
   }
    return  (<Modal isOpen={isModalOpen} onClose={() => onClose()} >
    {!isLogin ? 
        <Register />
        :
        <Login setisLogin={() => setisLogin(false)} />
    }
    </Modal>)
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are couple of ways you can achieve this. i am posting the useEffect way.

you can use useEffect and listen on the isLogin if it changes trigger the setisLogin() function.

code

const Navbar = () => {

  const [isModalOpen, setisModalOpen] = useState(true)
  const [isLogin, setisLogin] = useState(true)

  React.useEffect(() => {
     if(!isLogin){
        setisLogin(true);
     }
  },[isModalOpen])

  
  const onClose = () =>{
     setisModalOpen(false)
  }

 return  (<Modal isOpen={isModalOpen} onClose={() => onClose()} >

 {!isLogin ? 
    <Register />
    :
    <Login setisLogin={() => setisLogin(false)} />
  }
 </Modal>)
}

useState and setState both are asynchronous in nature already.

about 4 years ago · Juan Pablo Isaza Relatório

0

Like Shoyeb Memon mentioned, you can use React's useEffect hook to "listen" to changes with isModalOpen and act upon those changes.

The implementation should look like this:

const onClose = () =>{
   setisModalOpen(false)
}

useEffect(() => {
   if(!isLogin){
      setisLogin(true)
   }
}, [isModalOpen]);

This is the way we usually handle this types of situation in React,
where we need to act upon a change in state or in values that we receive asynchronously.

about 4 years ago · Juan Pablo Isaza Relatório
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