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

  • Thread starter Thread starter mathmari
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around creating a sidebar menu in a React application, specifically addressing issues related to displaying suboptions within the menu. Participants explore the structure of the menu items, the use of different types for menu options, and the integration of icons.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their initial implementation of a sidebar menu and notes an error message when including a suboption.
  • Another participant suggests checking the documentation for the menu component being used, indicating that the solution may depend on it.
  • A later reply proposes that the type for the option must be changed to "collapse" to display suboptions correctly.
  • One participant reports success after adding a "target: true" property, while another later states that removing this property also resolves the issue.
  • Participants discuss the use of icons, with one asking how to integrate them and another noting that "bi-person-fill" appears to be from Bootstrap Icons.
  • There is a mention of the need to understand the dependencies and documentation for various components in a modern web application.
  • One participant confirms they found a way to use icons by importing them from tabler/icons.
  • A question is raised about how to create a new page for a specific URL and whether it requires a new JavaScript file with a corresponding name.
  • Another participant suggests a potential structure for routing in React, mentioning the use of React Router.

Areas of Agreement / Disagreement

Participants express varying opinions on the necessity of the "target: true" property and the correct approach to integrating icons. The discussion remains unresolved regarding the best practices for routing and file structure in React applications.

Contextual Notes

There are references to specific libraries and components, but the exact dependencies and configurations are not fully established, leaving room for interpretation and further exploration.

Who May Find This Useful

Developers working with React who are interested in creating sidebar menus, managing routing, and integrating icons into their applications.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
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   Reactions: I like Serena
Technology news on Phys.org
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   Reactions: mathmari
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?
 
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? 🤔
 
I deleted now the target: true and it works also without that. 🤔
 
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? 🤔
 
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   Reactions: mathmari
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?
 
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   Reactions: 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   Reactions: mathmari

Similar threads

  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 4 ·
Replies
4
Views
5K
  • Sticky
  • · Replies 2 ·
Replies
2
Views
504K