Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!