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>