javascript - How to display submenu links in React - Stack Overflow

I am attempting to create a sidebar menu in React and I want to know what is the best way to display su

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
Add a ment  | 

1 Answer 1

Reset to default 3

Based 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 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745395780a4625869.html

相关推荐

  • javascript - How to display submenu links in React - Stack Overflow

    I am attempting to create a sidebar menu in React and I want to know what is the best way to display su

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信