React Navbar With Bootstrap 4
Tags: react, bootstrap, javascript
Published: 2020-08-26 11:58:02

---
title: React Navbar With Bootstrap 4
published: true
date: 2018-01-20 14:02:57 UTC
tags: ["react", "bootstrap", "javascript"]
canonical_url: https://www.turtle-techies.com/react-navbar-with-bootstrap-4/
---

## React And Bootstrap
There's a very small chance you haven't heard about <a href="https://reactjs.org/" target="_blank"> React</a> or <a href="https://getbootstrap.com" target="_blank">Bootstrap</a>.  
Both React and Bootstrap, are very very popular. In fact, they are so popular that there's a <a href="https://react-bootstrap.github.io/" target="_blank">project that combines them</a>.  

Since React is so popular, I decided I should be more familiar with it and also thought it would be nice to share my learnings.  

In this example, we will create a Bootstrap navbar without "react-bootstrap". The complete solution is on github and you can get it <a href="https://github.com/asafg6/react-bootstrap-navbar-example" target="_blank">here</a>.

## Prerequisites
I'll assume you are comfortable with HTML, Bootstrap, css and javascript. Entry level knowledge of React is required.  


## Step 1 - Setup
If you don't have React installed, install it now by following the instructions on the <a href="https://reactjs.org/docs/add-react-to-a-new-app.html" target="_blank">React website</a>.  

Create a new React app:  
```shell
$ create-react-app react-bootstrap-navbar-example
```
This generated some files for us in the react-bootstrap-navbar-example folder.

Great! Now, let's add Bootstrap to our new generated index.html file. The easiest way to do this is with the CDN, as documented <a href="https://getbootstrap.com/docs/4.0/getting-started/introduction/" target="_blank">here</a>.
Add the CDN link to the head section of index.html:   
**public/index.html**
```html
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <!-- Bootsrap! -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">


    <title>React App</title>
  </head>

```
Note that for this example, Bootstrap's javascript is not required.

We are going to use the 'react-router-dom' package, so go ahead and install it.  
In your project dir:  
```shell 
$ npm install react-router-dom --save
```

Start the React development server from the project folder like this:
```shell
$ npm start
```


## Step 2 - Add Navigation Links
We are going to have two sections:  

* The "main" section that will hold our content.
* The "header" section that will hold our navbar.

Go ahead and replace the contents of src/App.js with this code:  
```javascript
import React, { Component } from 'react';

const Main = () => (
  <div>
    main!
  </div>
)

const Header = () => (
  <div>
    header!
  </div>
)

const App = () => (
  <div>
    <Header />
    <Main />
  </div>
)

export default App;

```
As you can see, it doesn't do much yet. It just divides our page to two sections. If you point your browser to <a href="http://localhost:3000/" target="_blank">localhost:3000</a>, you should see the words "header!" and "main!" one on top of the other.  

Let's continue by adding our pages. Since this is only an example, they won't be too impressive.
Create a new file for our pages:
**src/pages.js**
```javascript
import React, { Component } from 'react';


export const Home = () => (
    <div>
        <h1>Home</h1>
    </div>
)

export const Page1 = () => (
    <div>
        <h1>Page1</h1>
    </div>
)

export const Page2 = () => (
    <div>
        <h1>Page2</h1>
    </div>
)

export const Page3 = () => (
    <div>
        <h1>Page3</h1>
    </div>
)
```

Change Our App in index.js to be a BrowserRouter:  
**src/index.js**
```javascript
import { BrowserRouter } from 'react-router-dom';

ReactDOM.render((
    <BrowserRouter>
      <App />
    </BrowserRouter>
  ), document.getElementById('root'))
```

Change the Main component in App.js to a Switch:  
**src/App.js**
```javascript
import { Switch, Route, Link } from 'react-router-dom'; // import the react-router-dom components
import { Home, Page1, Page2, Page3 } from './pages' // import our pages

const Main = () => (
  <main>
    <Switch>
      <Route exact path='/' component={Home} />
      <Route exact path='/1' component={Page1}/>
      <Route exact path='/2' component={Page2} />
      <Route exact path='/3' component={Page3} />
    </Switch>
  </main>
)

```

Change our Header component to show links:
**src/App.js**
```javascript

const Header = () => (
  <div>
    <ul>
      <li>
        <Link to="/">Home</Link>
      </li>
      <li>
        <Link to="/1">Page1</Link>
      </li>
      <li>
        <Link to="/2">Page2</Link>
      </li>
      <li>
        <Link to="/3">Page3</Link>
      </li>
    </ul>
  </div>
)

```
If you want to read more about React routers, there's a nice tutorial about it <a href="https://medium.com/@pshrmn/a-simple-react-router-v4-tutorial-7f23ff27adf" target="blank">here</a>.  

Alright! So we now have a functional, ugly website with routing.  


## Step 3 - Adding Bootstrap
Let's turn our ugly header to a Bootstrap navbar. Here's a <a href="https://getbootstrap.com/docs/4.0/components/navbar/" target="_blank">link to the navbar documentation</a>, in case you want to know more.  
Change the Header component in App.js:  
**src/App.js**
```javascript

const Header = () => (
  <div>
    <nav className="navbar navbar-expand-lg navbar-light  bg-light">
      <Link className="navbar-brand" to="/">Home</Link>
      <ul className="navbar-nav">
        <li className="nav-item">
          <Link className="nav-link" to="/1">Page1</Link>
        </li>
        <li className="nav-item">
          <Link className="nav-link" to="/2">Page2</Link>
        </li>
        <li className="nav-item">
          <Link className="nav-link" to="/3">Page3</Link>
        </li>
      </ul>
    </nav>
  </div>
)


```
Here is how it should look like:  

![The almost final result](https://dev-to-uploads.s3.amazonaws.com/i/jnwamb84e3prslu3v0pp.png)

Looks so much better!  


## Step 4 - Use Bootstrap's Active Class
Our page looks better already, but we would also want our links to appear as active in respect to the page we are on. To achieve this, we will turn our links to React components.  
Add the code for our new NavLink component in App.js:  
**src/App.js**
```javascript
class NavLink extends Component {
  render() {
      return (
        <li className="nav-item" >
                  <Link className="nav-link" to={this.props.path}>{this.props.text}</Link>
        </li>
      );
  }
}
```
Our NavLink will get the path and text as part of it's passed properties.  

Change our Header to use the new NavLink:  
**src/App.js**
```javascript

const Header = () => (
  <div>
    <nav className="navbar navbar-expand-lg navbar-light  bg-light">
      <Link className="navbar-brand" to="/">Home</Link>
      <ul className="navbar-nav">
        <NavLink path="/1" text="Page 1" />
        <NavLink path="/2" text="Page 2" />
        <NavLink path="/3" text="Page 3" />
      </ul>
    </nav>
  </div>
)

```
This will look exactly the same in the browser, but since we turned our simple html to a component, our code already looks nicer.  


Let's add the active functionality to NavLink:  
**src/App.js**
```javascript
class NavLink extends Component {

  render() {
      return (
        <li className={"nav-item " + (this.props.isActive ? "active": "")}>
                  <Link className="nav-link" to={this.props.path}>{this.props.text}</Link>
        </li>
      );
  }
}
```
Our NavLink will now render with the active class, in case we pass ```isActive=true``` to it.  
In React, when you have multiple stateful child components, it is better to "lift the state upwards".  
From the React tutorial:  

> When you want to aggregate data from multiple children or to have two child components 
> communicate with each other, move the state upwards so that it lives in the parent component. The 
> parent can then pass the state back down to the children via props, so that the child components 
> are always in sync with each other and with the parent.

Let's change our Header component so it could handle it's children's state:  
**src/App.js**
```javascript
class Header extends Component {

  constructor(props) {
    super(props);
    this.state = {
      links: [
        {path: "/1", text: "Page 1", isActive: false},
        {path: "/2", text: "Page 2", isActive: false},
        {path: "/3", text: "Page 3", isActive: false},
      ]
    }
  }

  handleClick(i) {
    const links = this.state.links.slice(); 
    for (const j in links) {
      links[j].isActive = i == j ;
    }
    this.setState({links: links});
  }


  render() {
    return (
      <div>
        <nav className="navbar navbar-expand-lg navbar-light  bg-light">
          <Link className="navbar-brand" to="/">Home</Link>
          <ul className="navbar-nav">
            {this.state.links.map((link, i) => 
              <NavLink 
                path={link.path} 
                text={link.text} 
                isActive={link.isActive}
                key={link.path} 
                onClick={() => this.handleClick(i)}
              /> 
              )}
          </ul>
        </nav>
      </div>
    );
  }
}
```

Add onClick to our NavLink, so the parent can pass it:  
**App.js**
```javascript
class NavLink extends Component {

  render() {
      return (
        <li className={"nav-item " + (this.props.isActive ? "active": "")}>
                  <Link 
                    className="nav-link" 
                    to={this.props.path}
                    onClick={() => this.props.onClick()}
                  >
              {this.props.text}</Link>
        </li>
      );
  }
}

```
Great! Now when you click on one of your links, the active class would be added.
It should look like this:  

![Final Result](https://dev-to-uploads.s3.amazonaws.com/i/dq4tm7q8unu5pa5fktp9.png)


## Summary
So... What did we do here?  
We created a React router, with header and main sections.  
We then added Bootstrap, making our website look nicer.
Finally we created our own NavLink component and saw how to "lift up the state".
I am sure that there are many solutions to this very common problem. Some of them might be less verbose, and even solve it in a line or two.  
I hope you enjoyed, and of course, feel free to comment or share.  
Thank you for reading.