React - Menu - Sidebar - Suboptions are not shown in browser

  • Thread starter mathmari
  • Start date
In summary: There are other routers for React: the first one I used was Reach Router, which is simpler to use but it doesn't have as many features as React Router. If you are using React Router, it would be easy to add a new route for Suboption1. In summary, the conversation is about creating a side menu using a JavaScript file named "menu.js" which contains code for a menu with different options and suboptions. One issue that arises is the "Menu Items Error" message when trying to run the code with the Suboption1. It is discovered that the "target: true" code is causing the issue and is not necessary for the menu to work properly. The conversation then shifts to discussing the use of
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :smile:

I want to create a side menu.

I have written the following in a file menu.js :

JavaScript:
const menu_items = {
    id: 'menu',
    title: 'Menu',
    type: 'group',
    children: [
        {
            id: 'Option1',
            title: 'Option 1',
            type: 'group',
            children: [
                {
                    id: 'SubOption1',
                    title: 'Suboption 1',
                    type: 'item',
                    url: '/Option-1/SubOption-1'
                }
            ]
        },
        {
            id: 'Option2',
            title: 'Option 2',
            type: 'item',
            url: '/Option-2'
        },
        {
            id: 'Option3',
            title: 'Option 3',
            type: 'item',
            url: '/Option-3'
        },
        {
            id: 'Option4',
            title: 'Option 4',
            type: 'item',
            url: '/Option-4'
        },
        {
            id: 'Option5',
            title: 'Option 5',
            type: 'item',
            url: '/Option-5'
        },
        {
            id: 'Profile',
            title: 'Profile',
            type: 'item',
            url: '/Profile'
        }
    ]
};
export default menu_items;

The code without the SubOption1 works properly.

When I run it with the Suboption 1 I get in the browser all Options and above the options I get in red letters "Menu Items Error" and the suboption is not shown. What am I doing wrong? 🤔
 
Last edited:
  • Like
Likes I like Serena
Technology news on Phys.org
  • #2
Please edit your post using the opening tag [code=javascript] instead of just [code].

But the answer depends on what menu component you are using, I suggest you check in the documentation for it.
 
  • Like
Likes mathmari
  • #3
pbuk said:
But the answer depends on what menu component you are using, I suggest you check in the documentation for it.

Ah the type item must be collapse, right?
 
  • #4
I added also "target: true" and now it works!

JavaScript:
const menu_items = {
    id: 'menu',
    title: 'Menu',
    type: 'group',
    children: [
        {
            id: 'Option1',
            title: 'Option 1',
            type: 'collapse',
            children: [
                {
                    id: 'SubOption1',
                    title: 'Suboption 1',
                    type: 'item',
                    url: '/Option-1/SubOption-1'
                    target: true
                }
            ]
        },
        {
            id: 'Option2',
            title: 'Option 2',
            type: 'collapse',
            url: '/Option-2'
        },
        {
            id: 'Option3',
            title: 'Option 3',
            type: 'collapse',
            url: '/Option-3'
        },
        {
            id: 'Option4',
            title: 'Option 4',
            type: 'collapse',
            url: '/Option-4'
        },
        {
            id: 'Option5',
            title: 'Option 5',
            type: 'collapse',
            url: '/Option-5'
        },
        {
            id: 'Profile',
            title: 'Profile',
            type: 'item',
            url: '/Profile'
        }
    ]
};
export default menu_items;

What does " target: true " mean? 🤔
 
  • #5
I deleted now the target: true and it works also without that. 🤔
 
  • #6
I have seen in a json file the icons before the above options, we have there fore example

"icon": "bi-person-fill"

In the JavaScript file do we use them writting:

import { Icon } from '@tabler/icons';

Or how do we use for example the above icon? 🤔
 
  • #7
mathmari said:
I deleted now the target: true and it works also without that.
Hey mathmari!

So it works now and whatever issue there was, is gone? 🤔
 
  • Like
Likes mathmari
  • #8
I like Serena said:
Hey mathmari!

So it works now and whatever issue there was, is gone? 🤔

Yes! It works! There is no problem now!

Do you have an idea about the icons?
 
  • #9
Do we have to search for the icons here ? 🤔
 
Last edited:
  • #10
mathmari said:
"icon": "bi-person-fill"
bi-person-fill looks like the name of an icon from Bootstrap Icons. Have you installed this, or are you using a UI framework that includes it?

mathmari said:
import { Icon } from '@tabler/icons';
@tabler/icons is a completely different icon package. Have you installed this, or are you using a UI framework that includes it?

You need to understand that a modern web application is built from many different components; it is YOUR responsibility to assemble the correct dependencies together and read their documentation to achieve what you want for the particular, possibly unique, combination you have chosen. Sites like PhysicsForums do not exist to replace this responsibility.
 
  • #11
I found it how to use the icons. I imported them from tabler/icons!

Thank you for your help! :smile:
 
  • Like
Likes I like Serena
  • #12
mathmari said:
JavaScript:
const menu_items = {
    id: 'menu',
    title: 'Menu',
    type: 'group',
    children: [
        {
            id: 'Option1',
            title: 'Option 1',
            type: 'group',
            children: [
                {
                    id: 'SubOption1',
                    title: 'Suboption 1',
                    type: 'item',
                    url: '/Option-1/SubOption-1'
                }
            ]
        },
        {
            id: 'Option2',
            title: 'Option 2',
            type: 'item',
            url: '/Option-2'
        },
        {
            id: 'Option3',
            title: 'Option 3',
            type: 'item',
            url: '/Option-3'
        },
        {
            id: 'Option4',
            title: 'Option 4',
            type: 'item',
            url: '/Option-4'
        },
        {
            id: 'Option5',
            title: 'Option 5',
            type: 'item',
            url: '/Option-5'
        },
        {
            id: 'Profile',
            title: 'Profile',
            type: 'item',
            url: '/Profile'
        }
    ]
};
export default menu_items;

When we want to create the page for example with url '/Option-1/SubOption-1' how do we do that? Do we create a new js file and this must have the name the same as the id above? And then we call that function to App.js ? 🤔
 
  • #13
Do we create a new js file with something like the following?

JavaScript:
import { lazy } from 'react';
import Loadable from 'ui-component/Loadable';
import MainLayout from 'layout/MainLayout';
const New = Loadable(lazy(() => import('pages/Suboption1')))

const SuboptionRoutes = {
    path: '/',
    element: <MainLayout />,
    children: [
        {
            path: '/Option-1/SubOption-1',
            element: <SubOption1 />
        }
    ]
};
export SuboptionRoutes;
 
  • #14
Have you already got routing working for /Profile, Option-2 etc?

React doesn't have a builtin router; are you using React Router? The tutorial there is pretty good.
 
  • Like
Likes mathmari

FAQ: React - Menu - Sidebar - Suboptions are not shown in browser

Why are my suboptions not showing in the sidebar menu?

There could be several reasons for this. One possibility is that the suboptions are not properly nested within the main menu item in the code. Another possibility is that there is a CSS rule that is hiding the suboptions. Check your code and CSS to make sure everything is correctly structured.

How do I add suboptions to my React sidebar menu?

To add suboptions, you need to nest them within the main menu item in your code. This can be done using a component or a simple HTML structure. You also need to add CSS rules to style the suboptions and make them visible in the sidebar.

Can I customize the appearance of the suboptions in my React sidebar menu?

Yes, you can customize the appearance of the suboptions by adding CSS rules to your code. You can change the font, color, size, and other properties to match your desired design. You can also add animations or effects to make the suboptions more visually appealing.

How can I make the suboptions in my React sidebar menu responsive?

To make the suboptions responsive, you can use CSS media queries to adjust the layout and styling of the suboptions based on the device or screen size. You can also use React libraries or components that support responsive menus.

Are there any pre-built React components or libraries for creating sidebar menus with suboptions?

Yes, there are several pre-built React components and libraries that offer sidebar menus with suboptions. Some popular options include React-Sidebar, React-Burger-Menu, and Material-UI. These components offer customizable options and easy integration with React projects.

Similar threads

Replies
2
Views
1K
Replies
2
Views
8K
Replies
4
Views
4K
  • Sticky
Replies
2
Views
497K
Back
Top