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

193
Vistas
React Custom Drop-Down - remove click event

I have made a custom dropDown with a div that I set an onClick attribute to open dropDown and close it. but also I want to close it when the user clicks to another part of the site.

 <div
    onClick={() => setIsDropDownOpen(!isDropDownOpen)}
    className="selected-drop-down"
 >
    <span className="dropDownText">{selectedQuoteCurrency}</span>
    <img
      className="dropDownIcon"
      src={require("../assets/image/arrow/dropDown-Arrow.png")}
      width="15px"
      alt="arrow"
    />
 </div>

I also do this logic

useEffect(() => {
    if(isDropDownOpen) {
      window.addEventListener('click',changeDD )
    }else {
      window.removeEventListener('click',changeDD )
    }
  },[isDropDownOpen])

  function changeDD() {
    setIsBaseDropDownOpen(false)
  }

But I was faced with a problem that when the component didmount for the first time everything is okay and dropDown opened and when click over there it closed, but for the second time, the dropdown didn't open because the state did not change to true.

furthermore, I tried to handle this click event by tabindex attribute according to this document => developerMozila but I didn't succeed

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use hover in css like this. when mouse go away from loc of dropdown menu, automatic close.

.options:hover{
    display: flex;
    flex-direction: column;
    position: absolute;
}
<div className="options">
   <button>Add</button>
   <button>Logout</button>
 </div>

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