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

148
Views
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 answers
Answer question

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 Report

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