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

179
Vistas
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 Respuestas
Responde la pregunta

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