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

200
Views
Click OutSide co close sidebar reactjs

I create a function when I click outside of the sidebar it will hide it and I also have a button that toggles show and hide the sidebar. But when I combined both of them together, the button did not work properly, it only show the sidebar but can't close it, only when I click outside it will close the sidebar

enter image description here

Click OutSide to close function:

     const ref = useRef(null);
  useEffect(() => {
    document.addEventListener("mousedown", Clickout);
    return () => {
      document.removeEventListener("mousedown", Clickout);
    };
  }, []);

  const Clickout = (eve) => {
    if (ref.current && !ref.current.contains(eve.target)) {
      setShow(false);
    }
  };

My Return:

 return (
    <header>
      <div className="head">
        <div className="logo">
          <img src={logo} alt="logo" />
        </div>
        <button
          className="burger"
          onClick={() => {
            setShow(!showMenu);
            console.log("here");
          }}
        >
          <div className={`${showMenu ? "change" : ""} bur1 `}></div>
          <div className={`${showMenu ? "change" : ""} bur2 `}></div>
          <div className={`${showMenu ? "change" : ""} bur3 `}></div>
        </button>
      </div>

      <nav className={showMenu ? "active" : ""} ref={ref}>
        <ul>
          {navItem.map((item) => {
            const { id, url, text } = item;
            return (
              <li key={id}>
                <a href={url}>{text}</a>
              </li>
            );
          })}
        </ul>
      </nav>
    </header>
  );
};

Nav bar CSS:

nav {
  position: fixed;
  right: -100%;
  top: 0;
  width: 60%;
  height: 100vh;
  text-align: center;
  padding-top: 15vh;
  transition: 0.8s ease;
  background-color: blue;
}

nav.active {
  right: 0;
  transition: 0.5s;
}

Thank you.

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

0

you can use another state for manage button onclick when menu is open:

  const [disableBtn, setDisableBtn] = useState(false);

and in Clickout function manage it:

const Clickout = (eve) => {
  if (showMenu && ref.current && !ref.current.contains(eve.target)) {
    setShow(false);
    setDisableBtn(true)
  } else {
    setDisableBtn(false)
  }
};

and in button for onclick use condition:

if (!disableBtn) setShow(true);
about 4 years ago · Juan Pablo Isaza Report

0

Updating state this way setShow(!showMenu) does not immediately update the state.Rather it schedules the update(You can read the docs). When your setState depends on your previous state (in this case showMenu depends on previous state) use this technique: (prev) => setState(!prev) instead. So, simply updating your onClick will solve the issue.

<button className="burger"
        onClick={() => {
            (prevShowMenu) => setShow(!prevShowMenu)
        }}>

(Let me know in the comments if this was helpful)

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!