Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using onClick with array map in Dynamic menu ( ReactJS)

I am creating a dynamic menu from an array but I only want an onClick method on the last item.This is in a ReactJS component. In the dropDownMenu.map, how do I setup the anchor tag to only have an onClick as defined in the array?

const dropDownMenu = [
    { name: 'Profile', href: '#' },
    { name: 'Settings', href: '#' },
    { name: 'Sign out', href: '#', onClick=logOut }
];

const logout = () => {
  signOut();
};

{dropDownMenu.map((item) => (
  <Menu.Item key={item.name}>
    {({ active }) => (
        <a href={item.href}>
          {item.name}
        </a>
    )}
  </Menu.Item>
))}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You don’t need to check anything in onClick it’s an extra useless code react handle this implicitly if the clickHandler is undefined. this link may help you.

const logout = () => {
  signOut();
};

const dropDownMenu = [
  { name: "Profile", href: "#" },
  { name: "Settings", href: "#" },
  { name: "Sign out", href: "#", clickHandler: logOut },
];

{
  dropDownMenu.map((item) => (
    <Menu.Item key={item.name}>
      {({ active }) => <a onClick={item.clickHandler} href={item.href}>{item.name}</a>}
    </Menu.Item>
  ));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading