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

178
Visualizações
React JS open a modal on button press if a user is not logged in

I'm trying to make a voting system like stackoverflow, when a user clicks a button to vote up / down, if he's not logged in, a pop-up modal is displayed. My user information is stored in context so I can use a hook to check if I have currently sign in user. The question is how can I properly make a check inside onClick handler.

My first suggestion was something like this.

function userVote(){

  const [showModal, setShowModal] = useState();

  const auth = useAuth();

  const handleClick = () => !auth.user ? setShowModal(true) : "the logic for updating the user 
  vote if he is already signed in"

  return (
    <>
      <button onClick={handleClick}></button>
      {showModal ? <Modal /> : null}
    </>
  )
}

But as far as I know, it is not a good practice to make state updates inside conditions or I'm wrong. I suppose there is a better way of doing this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

if you want to get rid of the condition before updating the state modify your function to be like:

const handleClick = () => {
  setShowModal(!auth.user)
  if (auth.user) {
  // Write the logics here
  }
}

it sets the state no matter if auth.user exists or not.

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