Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda