admin管理员组文章数量:1425188
I am attempting to create a sidebar menu in React and I want to know what is the best way to display submenu links/items based on the category? For example, I have 3 categories: "action", "edy", & "horror." Each of these would have submenu items, i.e. - "action" would have "Die Hard", "Ong Bak", and "John Wick."
Example:
Menu
Action
Die Hard
Ong Bak
John Wick
Comedy
Friday
Old School
This is the End
Horror
Exorcist
Midsomar
Hereditary
JSON:
[
{
type: 'action',
key: 'diehard',
name: 'Die Hard',
},
{
type: 'action',
key: 'ong-bak',
name: 'OngBak'
},
{
type: 'action',
key: 'johnwick',
name: 'JohnWick'
},
{
type: 'edy',
key: 'friday',
name: 'Friday'
},
{
type: 'edy',
key: 'old-school',
name: 'OldSchool'
},
{
type: 'edy',
key: 'this-is-the-end',
name: 'ThisistheEnd',
}
]
Currently, the way I have it set up won't display it the way I want to. But I wele any suggestions
import React from 'react';
import {Menu} from './menu/menu';
import {SubMenu} from './menu/submenu';
import {SubMenuItem} from './menu/submenuitem';
export function SideMenu({ routes }) {
return (
<SideBar>
<Menu>
{routes.map((route, index) => {
if (route.type === 'action') {
return (
<SubMenuItem
type={route.type}
key={route.key}
name={route.name}
onClick={onClickHandler}
/>
);
}
})}
</Menu>
</SideBar>
);
}
I am attempting to create a sidebar menu in React and I want to know what is the best way to display submenu links/items based on the category? For example, I have 3 categories: "action", "edy", & "horror." Each of these would have submenu items, i.e. - "action" would have "Die Hard", "Ong Bak", and "John Wick."
Example:
Menu
Action
Die Hard
Ong Bak
John Wick
Comedy
Friday
Old School
This is the End
Horror
Exorcist
Midsomar
Hereditary
JSON:
[
{
type: 'action',
key: 'diehard',
name: 'Die Hard',
},
{
type: 'action',
key: 'ong-bak',
name: 'OngBak'
},
{
type: 'action',
key: 'johnwick',
name: 'JohnWick'
},
{
type: 'edy',
key: 'friday',
name: 'Friday'
},
{
type: 'edy',
key: 'old-school',
name: 'OldSchool'
},
{
type: 'edy',
key: 'this-is-the-end',
name: 'ThisistheEnd',
}
]
Currently, the way I have it set up won't display it the way I want to. But I wele any suggestions
import React from 'react';
import {Menu} from './menu/menu';
import {SubMenu} from './menu/submenu';
import {SubMenuItem} from './menu/submenuitem';
export function SideMenu({ routes }) {
return (
<SideBar>
<Menu>
{routes.map((route, index) => {
if (route.type === 'action') {
return (
<SubMenuItem
type={route.type}
key={route.key}
name={route.name}
onClick={onClickHandler}
/>
);
}
})}
</Menu>
</SideBar>
);
}
Share
Improve this question
edited May 7, 2021 at 16:49
Etep
asked May 6, 2021 at 0:41
EtepEtep
3,6714 gold badges21 silver badges30 bronze badges
1 Answer
Reset to default 3Based on my experience, here is how I done this. I have done this before by creating a json data as the following example. Then, you can use js array map function to iterate each object and check whether the object has a key for subitem then render another style or code block accordingly. If your menu items all have subitem, maybe you may not have to do nested map function in this case. Note that the Submenu ponent will render the parent item then only render its children(subitem). I am using react-bootstrap to help me do the stylling.
Sample output:
All Movies
Action
本文标签:
javascriptHow to display submenu links in ReactStack Overflow
版权声明:本文标题:javascript - How to display submenu links in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745395779a2656805.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论