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

192
Visualizações
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 Respostas
Responde à pergunta

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