Home > OS >  How to pass path to Link if others items are null
How to pass path to Link if others items are null

Time:02-18

I have a array like this for the React Router Link, It is rendering as expected but when I click on any of the path the web is crashing, and in the console it's saying pathname is null.. How to make sure that even the link key is not provided it should work

const items = [{
    label: "Home"
    link: "/home"
  }, {
    label: "SubMenu"
    links: [{
      link: "/sub"
    }]
  },
  {
    label: "SubMenu1"
    links: [{
      link: "/sub1"
    }]
  }
]

I am using React router dom

after .map

<Link to={items.link}>
</Link>

CodePudding user response:

please try it:

const route=items.map((elem)=>(
<Link to={elem.link}>
{elem.label}
</Link>

CodePudding user response:

To be sure that it's not null, do this

<Link to={`${elem?.link}`}>
  {elem?.label}
</Link>
  • Related