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

156
Vistas
React - how to trigger function when <Link/> is navigated

As you can see in my code, there are many times for triggering the <MenuItem onClick={()=>{closeMainMenu()}}>.

That's why I want to see if there is any way to avoid this repetitive code

NarBar.js

import {
  ProSidebar,
  SidebarHeader,
  SidebarContent,
  Menu,
  MenuItem,
  SubMenu,
} from "react-pro-sidebar";
import { Link } from "react-router-dom";

import "react-pro-sidebar/dist/css/styles.css";

import { useSelector, useDispatch } from "react-redux";
import { closeMenu } from "../../Redux/actions";

export default function NavBar(props) {
  const dispatch = useDispatch();
  const showMenu = useSelector((state) => state.toggleMenu);

  const closeMainMenu = () => {
    dispatch(closeMenu())
  }
  return (
    <ProSidebar
      toggled={showMenu}
      onToggle={() => {
        closeMainMenu()
      }}
      breakPoint="md"
    >
      <SidebarHeader>
        <span>Proside Bar</span>
      </SidebarHeader>
      <SidebarContent>
        <Menu iconShape="square">
          <MenuItem onClick={()=>{closeMainMenu()}}>
            Dashboard
            <Link to="/" />
          </MenuItem>
          <MenuItem onClick={()=>{closeMainMenu()}}>
            General Information
            <Link to="/general-information" />
          </MenuItem>
          <MenuItem onClick={()=>{closeMainMenu()}}>
            blog
            <Link to="/blogs" />
          </MenuItem>
          <MenuItem onClick={()=>{closeMainMenu()}}>
            Users
            <Link to="/users" />
          </MenuItem>
        </Menu>
      </SidebarContent>
    </ProSidebar>
  );
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Why not just create a component?

const MenuLink = ({title, to}) => 
      <MenuItem onClick={()=>{closeMainMenu()}}>
        {title}
        <Link to={to} />
      </MenuItem>;

return ...
  <SidebarContent>
    <Menu iconShape="square">
      <MenuLink title="Dashboard" to="/" />
      <MenuLink title="General Information" to="/general-information" />
      <MenuLink title="blog" to="/blogs" />
      <MenuLink title="Users" to="/users" />
    </Menu>
  </SidebarContent>
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you just want to close the Menu whenever a Link is clicked on, you can create a new component:

const CloseMenu = () => {
  const dispatch = useDispatch();
  const {pathname} = useLocation();
  useEffect(() => {
    dispatch(closeMenu())
  }, [pathname]);
  return null;
};

export default CloseMenu;

and then add that to the top level of your router. Then the menu will close everytime the path changes and you won't need the onclick.

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