React JS is an excellent choice when it comes to building interactive user interfaces. It has gained immense popularity due to its ease of use, flexibility, and performance. One of the most common components in web development is the navigation bar, which is used to provide easy navigation through the website. React JS offers a wide range of tools and libraries that make it easy to create a navigation bar. One of the most popular and widely used libraries is Bootstrap, which offers a variety of components, including a Navbar.
In this tutorial, you will create a React JS Bootstrap Navbar Example. And will use the latest version of Bootstrap, i.e., Bootstrap 5, along with React JS to build a responsive navbar. The example we are going to create will have a basic structure that you can modify according to your needs.
How to Create and Use Bootstrap Navbar In React JS
Let’s follow the following steps to implement bootstrap navbar in react js app:
- Step 1 – Create React App
- Step 2 – Install Bootstrap 4
- Step 3 – Create Bootstrap Navbar Component
- Step 4 – Add Bootstrap Navbar Component in App.js
Step 1 – Create React App
In this step, open your terminal and execute the following command on your terminal to create a new react app:
npx create-react-app my-react-app
To run the React app, execute the following command on your terminal:
npm start
Check out your React app on this URL: localhost:3000
Step 2 – Install React Bootstrap
In this step, execute the following command to install react boostrap library into your react app:
npm install bootstrap --save npm install react-bootstrap bootstrap
Add bootstrap.min.css file in src/App.js
file:
import React, { Component } from 'react' import '../node_modules/bootstrap/dist/css/bootstrap.min.css'; function App() { return ( <div> <h2>How to Add Bootstrap Navbar in React</h2> </div> ); } export default App;
Step 3 – Create Bootstrap Navbar Component
Now, import “import { Navbar,Nav,NavDropdown,Form,FormControl,Button } from ‘react-bootstrap'” in navbar component.
Create BootstrapNavbar.js file. So, visit the src directory of your react js app and create a table component file named BootstrapNavbar.js. And add the following code into it:
import React from 'react' import { BrowserRouter as Router, Switch, Route, useParams, } from "react-router-dom"; import { Navbar,Nav,NavDropdown,Form,FormControl,Button } from 'react-bootstrap' import Home from './Home'; import AboutUs from './AboutUs'; import ContactUs from './ContactUs'; class BootstrapNavbar extends React.Component{ render(){ return( <div> <div className="row"> <div className="col-md-12"> <Router> <Navbar bg="dark" variant="dark" expand="lg" sticky="top"> <Navbar.Brand href="#home">React Bootstrap Navbar - Tutsmake.com</Navbar.Brand> <Navbar.Toggle aria-controls="basic-navbar-nav" /> <Navbar.Collapse id="basic-navbar-nav"> <Nav className="mr-auto"> <Nav.Link href="/">Home</Nav.Link> <Nav.Link href="/about-us">Contact Us</Nav.Link> <Nav.Link href="/contact-us">About Us</Nav.Link> <NavDropdown title="Dropdown" id="basic-nav-dropdown"> <NavDropdown.Item href="#action/3.1">Action</NavDropdown.Item> <NavDropdown.Item href="#action/3.2">Another action</NavDropdown.Item> <NavDropdown.Item href="#action/3.3">Something</NavDropdown.Item> <NavDropdown.Divider /> <NavDropdown.Item href="#action/3.4">Separated link</NavDropdown.Item> </NavDropdown> </Nav> <Form inline> <FormControl type="text" placeholder="Search" className="mr-sm-2" /> <Button variant="outline-success">Search</Button> </Form> </Navbar.Collapse> </Navbar> <br /> <Switch> <Route exact path="/"> <Home /> </Route> <Route path="/about-us"> <AboutUs /> </Route> <Route path="/contact-us"> <ContactUs /> </Route> </Switch> </Router> </div> </div> </div> ) } } export default BootstrapNavbar;
Step 4 – Add Bootstrap Navbar Component in App.js
In this step, you need to add BootstrapNavbar.js file in src/App.js
file:
import React from 'react'; import '../node_modules/bootstrap/dist/css/bootstrap.min.css'; import BootstrapNavbar from './BootstrapNavbar' function App() { return ( <div className="App"> <BootstrapNavbar /> </div> ); } export default App;
Conclusion
React bootstrap navbar example; In this tutorial, you have learned how to add bootstrap navbar in react js apps.