React Router Bootstrap Integration

0.26.3 · active · verified Sun Apr 19

This package provides seamless integration between React Router (specifically v6 and above in its current stable branch) and React-Bootstrap components. It allows developers to wrap standard React-Bootstrap elements like `Button`, `Nav.Link`, or `Dropdown.Item` within a `<LinkContainer>` component, effectively transforming them into functional navigation links that leverage React Router's routing capabilities. The current stable version is 0.26.3, with recent updates primarily focusing on bug fixes, indicating an active maintenance status. Its key utility is simplifying the process of creating navigable UI components from the React-Bootstrap library without the need for manual `onClick` event handling or direct `history` manipulation. It maintains separate installation paths and branches for compatibility with older React Router versions (v3, v4/v5), underscoring a critical version-based divergence for users to be aware of.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `react-router-bootstrap` with React Router v6 and React-Bootstrap components. It sets up a basic `Navbar` with navigation links using `<LinkContainer>` to route to different pages within a `BrowserRouter`.

import React from 'react';
import { BrowserRouter, Routes, Route, Outlet } from 'react-router-dom';
import { LinkContainer } from 'react-router-bootstrap';
import { Button, Nav, Navbar, Container } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';

const HomePage = () => <h2>Home Page Content</h2>;
const AboutPage = () => <h2>About Us Content</h2>;
const ContactPage = () => <h2>Contact Info Content</h2>;
const DashboardPage = () => <h2>Dashboard (Protected Route)</h2>;

const App = () => (
  <BrowserRouter>
    <Navbar bg="dark" variant="dark" expand="lg" className="mb-3">
      <Container>
        <Navbar.Brand href="#">My React App</Navbar.Brand>
        <Navbar.Toggle aria-controls="basic-navbar-nav" />
        <Navbar.Collapse id="basic-navbar-nav">
          <Nav className="me-auto">
            <LinkContainer to="/">
              <Nav.Link>Home</Nav.Link>
            </LinkContainer>
            <LinkContainer to="/about">
              <Nav.Link>About</Nav.Link>
            </LinkContainer>
            <LinkContainer to="/contact">
              <Nav.Link>Contact</Nav.Link>
            </LinkContainer>
          </Nav>
          <LinkContainer to="/dashboard">
            <Button variant="outline-info">Dashboard</Button>
          </LinkContainer>
        </Navbar.Collapse>
      </Container>
    </Navbar>

    <Container>
      <Routes>
        <Route path="/" element={<HomePage />} />
        <Route path="/about" element={<AboutPage />} />
        <Route path="/contact" element={<ContactPage />} />
        <Route path="/dashboard" element={<DashboardPage />} />
        <Route path="*" element={<h2>404 Not Found</h2>} />
      </Routes>
    </Container>
  </BrowserRouter>
);

export default App;

view raw JSON →