Open In App

How to add navigation links to the React Bootstrap Navbar?

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In ReactJS, we use Nabvar or Navigation Bar as the core component. For ease of navigation over the application, we use this NavBar in react-bootstrap. We need to add navigation links to go through different routes or pages of the application. So to add the navigation links to the React Bootstrap Navbar we can use the React Router, which is used to add the routes to the application.

Prerequisite:

Steps to create React Application and installing Bootstrap:

Step 1: Create a React application using the following command:

npx create-react-app nav-link

Step 2: After creating your project folder(i.e. nav-link), move to it by using the following command:

cd nav-link

Step 3: Now install react-bootstrap, react-icons, and react-router-dom in your working directory i.e. nav-link by executing the below command in the VScode terminal:

npm install react-bootstrap bootstrap react-icons react-router-dom

Step 4: Now we need to Add Bootstrap CSS to the index.js file:

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

Project Structure:

Example 1: In this example, we have imported the NavBar, Nav, and NavDropDown components from the react-bootstrap.

Javascript




//App.js
import React from "react";
import {
    BrowserRouter as Router,
    Route,
    Link,
    Routes,
    Outlet,
} from "react-router-dom";
import { Navbar, Nav, NavDropdown } from "react-bootstrap";
function GFGHome() {
    return (
        <div>
            <h2>GeeksforGeeks Home Page</h2>
            <p>Welcome to the GeeksforGeeks Home Page.</p>
        </div>
    );
}
function GFGAbout() {
    return (
        <div>
            <h2>GeeksforGeeks About Page</h2>
            <p>Learn more about GeeksforGeeks and its mission.</p>
        </div>
    );
}
function GFGContact() {
    return (
        <div>
            <h2>GeeksforGeeks Contact Page</h2>
            <p>Contact us for any inquiries or support.</p>
        </div>
    );
}
function NotFound() {
    return (
        <div>
            <h2>404 Not Found</h2>
            <p>Sorry, the page you are looking for does not exist.</p>
        </div>
    );
}
function GFGArticles() {
    return (
        <div>
            <h2>GeeksforGeeks Articles Page</h2>
            <p>
                Explore a wide range of informative articles on various computer
                science topics.
            </p>
        </div>
    );
}
function GFGCourses() {
    return (
        <div>
            <h2>GeeksforGeeks Courses Page</h2>
            <p>
                Enroll in comprehensive courses to enhance your coding skills
                and knowledge.
            </p>
        </div>
    );
}
function App() {
    return (
        <Router>
            <Navbar bg="light" expand="lg">
                <Navbar.Brand>
                    <h1 style={{ color: "green" }}>GeeksforGeeks</h1>
                </Navbar.Brand>
                <Navbar.Toggle aria-controls="basic-navbar-nav" />
                <Navbar.Collapse id="basic-navbar-nav">
                    <Nav className="ml-auto">
                        <Nav.Link as={Link} to="/" exact>
                            Home
                        </Nav.Link>
                        <Nav.Link as={Link} to="/about">
                            About
                        </Nav.Link>
                        <NavDropdown title="Resources">
                            <NavDropdown.Item as={Link} to="/articles">
                                Articles
                            </NavDropdown.Item>
                            <NavDropdown.Item as={Link} to="/courses">
                                Courses
                            </NavDropdown.Item>
                        </NavDropdown>
                        <Nav.Link as={Link} to="/contact">
                            Contact
                        </Nav.Link>
                    </Nav>
                </Navbar.Collapse>
            </Navbar>
            <div className="container mt-4">
                <Routes>
                    <Route path="/" element={<Outlet />}>
                        <Route index element={<GFGHome />} />
                        <Route path="/about" element={<GFGAbout />} />
                        <Route path="/contact" element={<GFGContact />} />
                        <Route path="/articles" element={<GFGArticles />} />
                        <Route path="/courses" element={<GFGCourses />} />
                        <Route path="*" element={<NotFound />} />
                    </Route>
                </Routes>
            </div>
        </Router>
    );
}
export default App;


Output:

Link

Example 2: In this example, we have made the Navigation Bar more attractive by adding the react-icons and more attractive styling into it.

Javascript




// App.js
import React from "react";
import {
    BrowserRouter as Router,
    Route,
    Link,
    Routes,
    Outlet,
} from "react-router-dom";
import { Navbar, Nav } from "react-bootstrap";
import "./App.css";
import { FaHome, FaInfoCircle, FaBook, FaPhone } from "react-icons/fa";
function GFGHome() {
    return (
        <div className="page">
            <h2>GeeksforGeeks Home Page</h2>
            <p>Welcome to the GeeksforGeeks Home Page.</p>
        </div>
    );
}
function GFGAbout() {
    return (
        <div className="page">
            <h2>GeeksforGeeks About Page</h2>
            <p>Learn more about GeeksforGeeks and its mission.</p>
        </div>
    );
}
function GFGContact() {
    return (
        <div className="page">
            <h2>GeeksforGeeks Contact Page</h2>
            <p>Contact us for any inquiries or support.</p>
        </div>
    );
}
function GFGArticles() {
    return (
        <div className="page">
            <h2>GeeksforGeeks Articles Page</h2>
            <p>
                Explore a wide range of informative articles on various computer
                science topics.
            </p>
        </div>
    );
}
function GFGCourses() {
    return (
        <div className="page">
            <h2>GeeksforGeeks Courses Page</h2>
            <p>
                Enroll in comprehensive courses to enhance your coding skills
                and knowledge.
            </p>
        </div>
    );
}
function App() {
    return (
        <Router>
            <Navbar className="navbar-bg" expand="lg">
                <Navbar.Brand>
                    <h1 className="navbar-brand-text">
                        <Link to="/" className="brand-link">
                            <FaHome /> GeeksforGeeks
                        </Link>
                    </h1>
                </Navbar.Brand>
                <Navbar.Toggle aria-controls="basic-navbar-nav" />
                <Navbar.Collapse id="basic-navbar-nav">
                    <Nav className="ml-auto">
                        <Nav.Link as={Link} to="/" className="nav-link">
                            <FaHome /> Home
                        </Nav.Link>
                        <Nav.Link as={Link} to="/about" className="nav-link">
                            <FaInfoCircle /> About
                        </Nav.Link>
                        <Nav.Link as={Link} to="/articles" className="nav-link">
                            <FaBook /> Articles
                        </Nav.Link>
                        <Nav.Link as={Link} to="/courses" className="nav-link">
                            <FaBook /> Courses
                        </Nav.Link>
                        <Nav.Link as={Link} to="/contact" className="nav-link">
                            <FaPhone /> Contact
                        </Nav.Link>
                    </Nav>
                </Navbar.Collapse>
            </Navbar>
            <div className="container mt-4">
                <Routes>
                    <Route path="/" element={<Outlet />}>
                        <Route index element={<GFGHome />} />
                        <Route path="/about" element={<GFGAbout />} />
                        <Route path="/contact" element={<GFGContact />} />
                        <Route path="/articles" element={<GFGArticles />} />
                        <Route path="/courses" element={<GFGCourses />} />
                    </Route>
                </Routes>
            </div>
        </Router>
    );
}
export default App;


CSS




/* App.css */
body {
  font-family: Arial, sans-serif;
  background-color: #f0f0f0;
  margin: 0;
  padding: 0;
}
.navbar-bg {
  background: linear-gradient(to right, #ff8b3d, #0abe22);
  color: rgb(255, 0, 0);
  transition: background-color 0.3s, color 0.3s;
  padding: 10px 0;
}
.navbar-brand-text {
  font-size: 24px;
  color: #4caf50;
  font-weight: bold;
}
.brand-link {
  text-decoration: none;
  color: white;
}
.nav-link {
  font-size: 16px;
  font-weight: bold;
  margin-right: 10px;
  color: rgb(255, 0, 0);
}
.nav-link:hover {
  color: #ffca28;
}
.nav-link {
  font-size: 16px;
  font-weight: bold;
  margin-right: 10px;
}
.page {
  background-color: #ffffff;
  padding: 20px;
  margin: 20px 0;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.page h2 {
  color: #2600ff;
  font-size: 24px;
  margin-bottom: 10px;
}
.page p {
  font-size: 16px;
  color: rgb(255, 0, 0);
}


Output:

Link2



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads