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

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

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 Relatório

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