Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive navigation menu with React and Bootstrap - hamburger isn't working

I created a simple navigation menu with Bootstrap. It's working fine in pure javascript, but when adapting it into react, the hamburger icon doesn't function (nothing happens on click). I installed bootstrap with

npm install --save bootstrap

And then added the bootstrap css to index.html in the public folder:

link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"

My jsx is as follows:

       <nav className="navbar navbar-expand-sm navbar-dark bg-dark">
        <div className="container">
          <button className="navbar-toggler" data-toggle="collapse" data-target="#navbarNav"><span className="navbar-toggler-icon"></span></button>
          <div className="collapse navbar-collapse" id="navbarNav">
            <ul className="navbar-nav ml-auto">
              <li className="nav-item">
                <Link to="/app/portfolio" className="nav-link">PORTFOLIO</Link>
              </li>
              <li className="nav-item">
                <Link to="/app/about" className="nav-link">ABOUT</Link>
              </li>
              <li className="nav-item">
                <Link to="#create-head-section" className="nav-link" style={{fontStyle: 'italic'}}>Personal art</Link>
              </li>
              <li className="nav-item">
                <Link to="#share-head-section" className="nav-link">CONTACT</Link>
              </li>
            </ul>
        </div>
        </div>
      </nav>

Again, everything looks fine except that the hamburger icon is not functioning.

like image 947
sir-haver Avatar asked Oct 28 '25 16:10

sir-haver


2 Answers

The bootstrap show class is used to display the collapsible menu items. So, the task is to simply add the show class conditionally on click of the hamburger icon. Just follow these simple steps to achieve this functionality.

Add a showCollapsedMenu(the name is up to you) property with initial value of false in your state like this:

state={
    showCollapsedMenu: false
}

Then declare a function like this which when called will reverse the current state:

toggleMenu = () => {
    this.setState({
      showCollapsedMenu: !this.state.showCollapsedMenu
    })
}

The above function will be called whenever the hamburger icon is clicked. So implement the onCLick method on the hamburger icon like this:

<button
  className="navbar-toggler"
  type="button"
  onClick={this.toggleMenu}    // <=
  data-toggle="collapse"
  data-target="#navbarNav">
  <span className="navbar-toggler-icon"></span>
</button>

Now create a const show which will conditionally add the show class depending on the state of showCollapsedMenu:

const show = (this.state.showCollapsedMenu) ? "show" : "" ;

Now finally add this show to the div with collapse navbar class like this:

<div className={"collapse navbar-collapse " + show} id="navbarNav">

Note: Mixing jQuery with React is not recommended as they both manipulate the DOM differently. While it may seem an easy solution, it might result in bigger problems.

like image 169
Ankush Chauhan Avatar answered Oct 31 '25 07:10

Ankush Chauhan


Bootstrap events require jQuery, Popper and Bootstrap.js source. That page will also let you know which components require JS. You can include jQuery, Popper and bootstrap.js in the index.html file, where you load your bundle. Either add that, or simply check out Reactstrap, which implements Bootstrap components in React.

like image 21
Predrag Beocanin Avatar answered Oct 31 '25 08:10

Predrag Beocanin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!