How can I show a dropdown menu in React.js?

I am currently developing an application using reactjs and incorporating the reactstrap css framework from Bootstrap. My goal is to create a navbar with menu and sub-menu functionality, restricted to just one level of sub-menus.

View Working Demo

My aim is to accomplish this within a single JS file,

Example.JS

<UncontrolledButtonDropdown>
   <DropdownToggle caret size="md">
      Menu 1
   </DropdownToggle>

   <DropdownMenu>
      <span> Sub Menu 1 </span>
      <DropdownItem>Sub Menu 1.1</DropdownItem>
      <DropdownItem>Sub Menu 1.2</DropdownItem>

      <span> Sub Menu 2 </span>
      <DropdownItem>Sub Menu 2.1</DropdownItem>
      <DropdownItem>Sub Menu 2.2</DropdownItem>
   </DropdownMenu>
</UncontrolledButtonDropdown>

I specifically require the above portion alone.

When the toggler button is clicked, a dropdown menu will list sub-menus separately in another view. The parent menu will be labeled as Menu 1.

In the regular view, the layout remains unchanged, but for mobile or responsive views, the menu should display in an organized manner.

The resulting appearance should resemble a navigation bar featuring a dropdown menu.

https://i.sstatic.net/hGByX.png

Upon clicking on the parent menu (Menu 1), the Sub Menu 1 section should be displayed independently as follows:

The dropdown menu listing sub-menus separately in another view

https://i.sstatic.net/1e6I1.png

This demonstrates that when selecting a menu, the corresponding sub-menu should appear separately in another view without the use of jQuery, accomplished purely through Bootstrap.

Answer №1

To ensure a responsive dropdown menu, one may consider avoiding the reactstrap library and instead opt for utilizing the bootstrap library. Detailed guidance on rendering a responsive dropdown can be found by visiting this link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can a HTML div be created with an animation and a hover-scale effect added to it?

I experimented with using divs as clickable buttons that direct to another page on my website. I added animations to make them rotate into view when the site is loaded. Now, I'm trying to implement a hover effect that will scale the buttons when hover ...

Unlock the capability to automatically swipe on a React Native TextInput Carousel while the user types

My challenge involves setting up a carousel with either Flatlist or ScrollView (I have tested both options). This Carousel consists of multiple TextInputs. The goal is to achieve the following: There are 4 TextInputs. When the user enters 6 digits in the ...

The ApexChart Candlestick remains static and does not refresh with every change in state

I am currently working on a Chart component that retrieves chart data from two different sources. The first source provides historic candlestick data every minute, while the second one offers real-time data of the current candlestick. Both these sources up ...

What could be causing the lack of value appearing after I clicked the button?

Setting the value for the array of images as an empty string does not return the expected result. When I move one of the 4 images and click the button, I anticipate a new array with the information of one of the four images including src, x, and y coordina ...

Looking for a way to include an input box within a react-select option dropdown menu

Currently, I am facing a situation where I need to include an input box within my dropdown option menu. The challenge is that I cannot utilize the custom tag creator feature of react select to create a new option with the dropdown. Upon going through the ...

Tips for displaying the Bootstrap Navbar in a single line on the xs layout

I am working on a Bootstrap navbar that displays properly in most screen layouts, but I am having trouble getting it to show on one line in the xs layout. Even though there is enough room for it to fit on one line, it keeps breaking up into multiple lines. ...

Is there a way to adjust the selected color of a TextField?

I'm currently working with the Material-UI React library and I am trying to customize the border color of a TextField when it is clicked or in focus. This is my attempt so far: const useStyles = makeStyles((theme) => ({ input: { borderWidth: ...

Securing user access and storing authentication tokens in NextJS

Currently utilizing NextJS for Server-side Rendering Our authentication method involves the use of JWT In a standard React application, we can employ JWT for each server request to retrieve data However, some pages return varied data based on whether th ...

Creating a Stable Left Navigation Bar with Bootstrap CSS

I am currently developing a website using Bootstrap CSS and I'm aiming for it to have a similar layout to the Bootstrap Dashboard example. However, I've encountered an issue with the right section of the fixed sidebar scrolling horizontally when ...

Shift the "Login link" to the right side of the Bootstrap navbar

Currently working on a Django website and incorporating Bootstrap/CSS/HTML to enhance the design. I've set up a navbar menu and want the login tab on the right side. However, despite my attempts, I'm not achieving the desired outcome: The HTML ...

The Next.js Image component does not implement the maxWidth attribute

<Image src="/assets/blog/image.webp" style={{ maxWidth: "700px" }} width={1400} height={1400} /> Issue Detected The image with src '/assets/blog/image.webp' has the following styles applied, but they are being overwrit ...

Determining the button's width relative to its parent's size using a

I'm trying to make my button take up 90% of the width of the screen. After testing my HTML code, I noticed that the button is only occupying around 10% of the screen. However, when I specify the width using pixels, it adjusts correctly. What could b ...

What is the most effective way to prevent a <pre> element from overflowing and instead horizontally scrolling when placed within a table cell that is not

I am faced with the following situation: <table> <tbody> <tr> <td> <pre style="overflow: scroll;"> (very long lines of text) Find my JSFiddle here: https://jsfiddle ...

Using a parent Id to implement React Material UI classes

How can I assign an id to a Material UI class when React renders it? For example: from .MuiInputLabel-outlined { z-index: 1; transform: translate(14px, 20px) scale(1); pointer-events: none; } to #root-id-here .MuiInputLabel-outlined { z- ...

In order to successfully render on the server side, it is essential to provide the userAgent in the muiTheme context when using the React Starter

When incorporating React Starter Kit with Material UI, I follow these steps: npm install material-ui --save After adding the following import to a component: import RaisedButton from 'material-ui/lib/raised-button'; and using: <RaisedButt ...

Align the text to the center within the Paper component using React and Material-ui

I've been struggling to center text inside a paper component in Material UI, and nothing seems to be working. I attempted using display:flex in the parent component with align-items:center in the child component. I also tried adding padding:5px for eq ...

Why is my CSS media query failing to increase font size?

I can't seem to get the "media query" function to work properly. I've tried multiple times and searched for a solution without any luck. Below is the code snippet where I'm attempting to increase the font size from 44px to 70px on small devi ...

Keep the Bootstrap modal open while the JavaScript validation is running, and redirect the form action if the validation is successful

When trying to execute the submit event from a form, my code is supposed to keep the modal open if validation fails. However, for some reason, this functionality is not working as expected. var myForm = document.updteform; var condition = true; if ...

Remove bulleted list from HTML using JavaScript DOM

My task involved deleting the entire "agent" array using the removeChild() function, requiring the id of a <ul> element. However, I faced an issue as I couldn't set the id for the "ul" due to using the createElement() function. //old code < ...

The hover effect is not resizing the button as expected

I attempted to create a similar effect like this: https://i.sstatic.net/hbiOS.gif As you can see, when the mouse hovers over the button, it has a scaling and fading white effect. I tried to achieve the same effect on my Bootstrap button by customizing m ...