Issue with Navbar collapse button not displaying items on Bootstrap 4.5.0 in combination with Next.js version 9.4.4

I'm currently working on a nextjs demo project where I'm incorporating bootstrap for styling purposes. The initial setup was straightforward as I installed bootstrap using npm and included it in my app.js.

import 'bootstrap/dist/css/bootstrap.min.css'

The navbar creation went smoothly, but I encountered an issue when trying to implement the collapsible feature for smaller screens.

After some investigation, I found out that I needed both popperjs and jquery in a specific order as mentioned here. So, I added these imports to my app.js file:

import 'jquery/dist/jquery.min.js'
import '@popperjs/core/dist/umd/popper.min.js'
import 'bootstrap/dist/css/bootstrap.min.css'

Despite making these changes, the toggle functionality still didn't work correctly. There were no errors displayed in either the browser console or my development environment.

Here are some of the dependencies listed in my package.json:

"dependencies": {
    "@popperjs/core": "^2.4.2",
    "bootstrap": "^4.5.0",
    "jquery": "^3.5.1",
    "next": "9.4.4",
    "react": "16.13.1",
    "react-bootstrap": "^1.0.1",
    "react-dom": "16.13.1"
  }

I searched Stack Overflow for similar issues, but most solutions referred to older versions of Bootstrap with different class names for components.

Below is a snippet of the component I'm working on:


/**
 * className to create the navbar using bootstrap, TODO, intial version
 */
export default class NavBar extends Component {
  render() {
    return (
      <div id="rootdiv">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#navbarTogglerDemo01"
            aria-controls="navbarTogglerDemo01"
            aria-expanded="false"
            aria-label="Toggle navigation"
          >
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
            <a class="navbar-brand" href="#">
              Hidden brand
            </a>
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
              <li class="nav-item active">
                <a class="nav-link" href="#">
                  Home <span class="sr-only">(current)</span>
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  Link
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link disabled" href="#">
                  Disabled
                </a>
              </li>
            </ul>
          </div>
        </nav>
      </div>
    );
  }
}

Answer №1

After encountering issues with native bootstrap, I switched to react-bootstrap and was able to resolve the problem. Hopefully, this solution will be beneficial for others facing a similar issue in the future.

It's worth noting that there is no need to manually import popper and jquery in the app.js file. Simply importing the minified bootstrap file suffices:

import 'bootstrap/dist/css/bootstrap.min.css'
import Navbar from "react-bootstrap/Navbar";
import Nav from "react-bootstrap/Nav";
import React from "react";

const RBNavBar = () => {
  return (
    <Navbar bg="light" expand="lg" id="myNavbar">
      <Navbar.Brand href="#home"><img src="/logo_lpb_small.png"></img></Navbar.Brand>
      <Navbar.Toggle aria-controls="basic-navbar-nav" />
      <Navbar.Collapse id="basic-navbar-nav">
        <Nav className="ml-auto" id="myNavItem">
          <Nav.Link href="/" id="myNavItem">Home</Nav.Link>
          <Nav.Link href="contact" id= "myNavItem">Contact</Nav.Link>
          <Nav.Link href="about" id= "myNavItem">About Us</Nav.Link>
        </Nav>
      </Navbar.Collapse>
    </Navbar>
  );
};

export default RBNavBar;

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

The background image will expand to fill any available space

I'm currently working on rendering divs with a background image CSS property. The images have fixed sizes, and I want to display them in a grid layout. However, the divs with background images stretch when there is extra space available, which is not ...

Is there a way to remove deleted SASS styles from the generated CSS?

Is it possible to remove markups from a generated css file using sass? For instance, in my scss file I have: body{ background:$dark; } The resulting CSS file looks like this: body{ background: #000; } If I delete that line of code in sass, what will h ...

Exploring the possibilities of querying Firestore data with dynamic user input

I am facing an issue with my code and struggling to find the right solution. My goal is to build a filter that, upon clicking on each option, will automatically fetch data from firestore. https://i.sstatic.net/ktLuE.png Within my Redux Saga, I have the ...

Keeping search functionality intact while transferring HTML to the label value in options with React-Select

I am facing an issue with a react-select field that includes HTML in the label value. Prior to adding HTML, searching works fine, but once I add HTML, the search functionality stops working. Is there a specific solution to make the field searchable while i ...

Utilizing React, Express, and NodeJS to submit data via a POST request to an API

Having some trouble with my client application's POST request to the API. I'm using React, Express, NodeJS, and MySQL in my stack. Tested inserting data directly into the database through Express and it works fine, but the client request isn&apos ...

Mount components based on their return value only if certain conditions are met

Is there a method to verify if a component renders null ? const ComponentX = ({ shouldDisplay }) => { if(!shouldDisplay){ return null; } return <div>Hello</div> } const ComponentY = () => <div>World</div> // I ...

What is the best way to customize the appearance of Image tags located inside SVGs, whether they are in separate files or embedded within the code

I am developing a custom SVG editor application and facing an issue with implementing a highlighted effect when an <image> element is clicked. I have successfully accomplished this for other elements like <circle> by adjusting attributes such a ...

Button activates SVG animation

Currently, I am working on an animation for a button using SVG. The animation utilizes the classes .is-loading and .is-success. However, when I click on the button, the animation does not execute as expected. I'm having trouble identifying the error w ...

Is it possible to hide a div using Media Queries until the screen size becomes small enough?

Whenever the screen size shrinks, my navbar sections start getting closer together until they eventually disappear. I've implemented a button for a dropdown menu to replace the navbar links, but I can't seem to make it visible only on screens wit ...

How can you define rules for font fallbacks that are specific to each font?

There are times when web fonts fail to load, especially if hosted on platforms like Google Fonts. In such cases, fallback fonts may require special attention as they can vary significantly from the originally specified fonts. For example: font-family:&ap ...

Ribbon design in LESS/CSS does not maintain its structure when resized in Google

My CSS/LESS ribbon is displaying perfectly in Firefox, but encountering issues in Chrome when resizing the window. At 100% zoom, everything looks good, but adjusting the zoom causes elements to become misaligned. To make it easier to troubleshoot, I' ...

I am experiencing issues with the middleware not functioning properly after implementing a custom application with Next.js

I'm currently diving into Next.js version 13 and attempting to customize the app based on the standard documentation. However, it seems that the middleware isn't being invoked as expected. I suspect there might be something wrong with my implemen ...

Utilize CSS in the same way as the legend tag styling

I'm looking to create a stylish border around my HTML component. While I know the HTML legend element can achieve this, I want to use it outside of a form. For reference on using HTML Legend: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml ...

Symbol '%' is not supported in Internet Explorer

My experience with IE versions 8 and 9 has been that they do not recognize the &percnt; entity. I have tested this on two different computers. I found information suggesting that it should be supported in IE here: http://code.google.com/p/doctype/wiki ...

React-Native introduces a new container powered by VirtualizedList

Upon updating to react-native 0.61, a plethora of warnings have started appearing: There are VirtualizedLists nested inside plain ScrollViews with the same orientation - it's recommended to avoid this and use another VirtualizedList-backed container ...

Encountering numerous errors when attempting to incorporate lottie-web into my JavaScript file

I am in the process of creating a unique audio player for my website utilizing HTML, CSS, and JavaScript. I encountered some challenges while trying to get it to work effectively on Codepen as well as my text editor. The animations were not functioning pro ...

How to transform the bootstrap 4 hamburger icon into a three dots menu icon using css only

The standard bootstrap 4 navigation bar button showcases a classic hamburger icon. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample03" aria-controls="navbarsExample03" aria-expanded="false" aria-label="T ...

What could be causing the MUI dialog to remain open despite clicking the button?

I am facing an issue in my React project with a dialog that opens full screen but does not close when clicking on the close icon button. The component responsible for opening the Dialog : interface IContentCardProps { content: IContentProps; } const Co ...

Can a 1D array be utilized to create a 2D grid in React?

I needed to display a one-dimensional array in a 2D grid format with 3 rows and 3 columns. The array itself contains numbers from 1 to 9. const array = Array.from({ length: 9 }, (_, i) => i + 1); In my React component, I have it rendering as a series o ...

Enlarge an image when hovering over it

I am currently in the process of creating a website and I have encountered an issue. When I hover over an image, I want it to zoom in, but unfortunately it does not zoom within the designated div area. How can I fix this problem? <div class="container" ...