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

98
Views
How to close dropdown menu using own useOnOutsideClick hook?

I faced a problem with my mobile drowdown menu. I'd like to close dropdown by clicking outside (somewhere, let's say on titels above) of my button and dropdown items.

This is my example.

Menu

const Menu = () => {
  const [isMobileNavOpen, setMobileNavState] = useState(false);

  const mobileNavRef = useRef();

  useOnOutsideClick(mobileNavRef, () => {
    if (isMobileNavOpen) setMobileNavState(false);
  });

  const toggleMobileNav = () => {
    setMobileNavState(!isMobileNavOpen);
  };

  return (
    <div>
      <div className="dropdown" ref={mobileNavRef}>
        <button
          className="dropdown-btn"
          onClick={toggleMobileNav}
          type="button"
        >
          DROPDOWN BUTTON
        </button>
        {isMobileNavOpen && (
          <div
            className="dropdown-menu"
            onClick={() => setMobileNavState(false)}
          >
            <a className="dropdown-item" href="#">
              Item1
            </a>
            <a className="dropdown-item" href="#">
              Item2
            </a>
          </div>
        )}
      </div>
    </div>
  );
};

my hook useOnClickOutside

export const useOnOutsideClick = (ref, callback) => {
  const handleClick = (e) => {
    if (ref.current && ref.current.contains(e.target)) {
      callback();
      console.log("click");
    }
  };

  useEffect(() => {
    document.addEventListener("click", handleClick);

    return () => {
      document.removeEventListener("click", handleClick);
    };
  });
};

Please about any help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Flip the contains logic of your useOutsideClick hook. You want to check of the click event originated from not within the target element the ref is attached to.

const handleClick = (e) => {
  if (!ref.current?.contains(e.target)) {
    callback();
    console.log("click");
  }
};

Edit how-to-close-dropdown-menu-using-own-useonoutsideclick-hook

Entire hook

export const useOnOutsideClick = (ref, callback) => {
  const handleClick = (e) => {
    if (!ref.current?.contains(e.target)) {
      callback();
      console.log("click");
    }
  };

  useEffect(() => {
    document.addEventListener("click", handleClick);

    return () => {
      document.removeEventListener("click", handleClick);
    };
  });
};
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!