I am building a multilevel menu and want to use recursion to display the menu. Something like the multi-level dropdown from here.
In my code my menu looks like this.
const menu = [
{
name: 'Man Utd',
menu: [
{
name: 'Stadium'
},
{
name: 'About'
}
]
},
{
name: 'Liverpool',
menu: [
{
name: 'Contact'
}
]
}
];
which I then pass this into my react component.
const Dropdown = ({ menu }) => {
const renderMenu = (items) => {
return items.map((item: any, index: any) => {
return (
<div>
{item.menu ? renderMenu() : item.name}
</div>
)
})
}
return (renderMenu(menu));
}
the issue here is that it is causing an infinite loop.
Can anyone advise me as to how I can improve this?