Lesson 40 of 60 – React Router Introduction
67%

React Router Introduction

React Router is a popular routing library used with React applications. It allows you to create multiple views or pages and navigate between them without performing a full browser page reload.

Note: React itself does not provide traditional application routing. React Router provides tools for creating client-side navigation and managing routes.

1. What is React Router?

React Router is a routing library used to manage navigation in React applications.

It allows different URLs to display different React components.

/home
/about
/contact
/products

Each URL can be associated with a particular component.

2. Why Do We Need Routing?

A real-world application usually contains multiple views such as:

  • Home
  • About
  • Contact
  • Products
  • Login
  • Dashboard

Routing allows users to move between these views using different URLs.

3. Single Page Application

React applications are commonly built as Single Page Applications (SPAs).

In an SPA, the browser can update the displayed content without requesting a completely new HTML document for every navigation.

Browser
   ↓
React Application
   ↓
Route
   ↓
Component

4. Client-Side Routing

React Router provides client-side routing.

The application can respond to URL changes and render the appropriate component without requiring a traditional full-page navigation for each route.

/about → About component
/contact → Contact component

5. Installing React Router

React Router can be installed using npm.

npm install react-router-dom

The package is commonly used for routing in React applications running in the browser.

6. react-router-dom

The react-router-dom package provides routing features for React applications that run in web browsers.

After installation, routing components and hooks can be imported from the package.

import {
    BrowserRouter,
    Routes,
    Route
} from "react-router-dom";

7. BrowserRouter

BrowserRouter provides the routing context for a browser-based React application.

import { BrowserRouter } from "react-router-dom";

function App() {
    return (
        <BrowserRouter>
            <h1>My React App</h1>
        </BrowserRouter>
    );
}

Components that use routing features are normally placed inside the router.

8. Routes

The Routes component contains the route definitions for an application.

<Routes>

    {/* routes go here */}

</Routes>

Individual Route components are normally placed inside Routes.

9. Route

A Route connects a URL path with a React element.

<Route
    path="/about"
    element={<About />}
/>

When the URL matches /about, the About element can be rendered.

10. Basic Router Structure

A basic React Router structure can look like this:

import {
    BrowserRouter,
    Routes,
    Route
} from "react-router-dom";

function App() {

    return (
        <BrowserRouter>

            <Routes>

                <Route
                    path="/"
                    element={<Home />}
                />

                <Route
                    path="/about"
                    element={<About />}
                />

            </Routes>

        </BrowserRouter>
    );
}

11. Creating a Home Component

A route can render a normal React component.

function Home() {
    return (
        <h1>Home Page</h1>
    );
}

export default Home;

This component can then be connected to the home route.

12. Creating an About Component

function About() {
    return (
        <h1>About Page</h1>
    );
}

export default About;

The component can be displayed when the user visits the corresponding route.

13. Multiple Routes

An application can contain multiple routes.

<Routes>

    <Route
        path="/"
        element={<Home />}
    />

    <Route
        path="/about"
        element={<About />}
    />

    <Route
        path="/contact"
        element={<Contact />}
    />

</Routes>

14. The Root Route

The path / commonly represents the home page.

<Route
    path="/"
    element={<Home />}
/>

When the application URL points to the root path, the Home component can be displayed.

15. Creating a Navigation Menu

A navigation menu provides links that allow users to move between routes.

function Navbar() {

    return (
        <nav>
            <a href="/">Home</a>
            <a href="/about">About</a>
        </nav>
    );
}

React Router also provides a special component called Link for application navigation.

16. Link Component

The Link component is used to create navigation links in a React Router application.

import { Link } from "react-router-dom";

function Navbar() {

    return (
        <nav>

            <Link to="/">Home</Link>

            <Link to="/about">About</Link>

        </nav>
    );
}

17. The to Attribute

The to prop tells the Link component which route should be opened.

<Link to="/contact">
    Contact
</Link>

Here, clicking the link navigates to the /contact route.

18. Link vs HTML Anchor

Traditional HTML navigation commonly uses an anchor element:

<a href="/about">About</a>

React Router provides:

<Link to="/about">About</Link>

For navigation within a React Router application, Link is commonly used because it integrates with client-side routing.

19. Navigation Without Full Page Reload

React Router's navigation components allow the application to change the displayed route while keeping the React application active.

<Link to="/products">
    Products
</Link>

This is one of the important benefits of client-side routing.

20. Organizing Route Components

For larger applications, pages can be separated into folders.

src/
├── pages/
│   ├── Home.jsx
│   ├── About.jsx
│   └── Contact.jsx
│
├── components/
│   └── Navbar.jsx
│
└── App.jsx

This organization can make routing and application maintenance easier.

21. Importing Page Components

Page components can be imported into the component that defines the routes.

import Home from "./pages/Home";
import About from "./pages/About";
import Contact from "./pages/Contact";

These components can then be used inside Route definitions.

22. Complete Basic Example

import {
    BrowserRouter,
    Routes,
    Route,
    Link
} from "react-router-dom";

function Home() {
    return <h2>Home</h2>;
}

function About() {
    return <h2>About</h2>;
}

function App() {

    return (
        <BrowserRouter>

            <nav>

                <Link to="/">
                    Home
                </Link>

                {" "}

                <Link to="/about">
                    About
                </Link>

            </nav>

            <Routes>

                <Route
                    path="/"
                    element={<Home />}
                />

                <Route
                    path="/about"
                    element={<About />}
                />

            </Routes>

        </BrowserRouter>
    );
}

export default App;

23. Route Matching

React Router checks the current URL and determines which route matches the URL.

/about

If the application contains:

<Route
    path="/about"
    element={<About />}
/>

The About element is rendered for that route.

24. Common Routing Structure

A simple application may have routes such as:

/              → Home
/about         → About
/services      → Services
/contact       → Contact
/login         → Login
/dashboard     → Dashboard

Each route can be connected to a separate React component.

25. Router in main.jsx

The router can also be placed around the application at the entry point. For example:

import React from "react";
import ReactDOM from "react-dom/client";
import {
    BrowserRouter
} from "react-router-dom";

import App from "./App";

ReactDOM.createRoot(
    document.getElementById("root")
).render(
    <BrowserRouter>
        <App />
    </BrowserRouter>
);

In this structure, routing components inside App can use the router context.

26. Router and Components

Components such as Link, Routes, and Route need to be used within the appropriate router context.

<BrowserRouter>

    <Navbar />

    <Routes>
        <Route
            path="/"
            element={<Home />}
        />
    </Routes>

</BrowserRouter>

27. Why React Router is Useful

  • Creates multiple application routes.
  • Connects URLs with React components.
  • Supports client-side navigation.
  • Helps build Single Page Applications.
  • Provides navigation components.
  • Supports nested and dynamic routes.
  • Can be used for authentication-based navigation.
  • Helps organize larger React applications.

28. Common React Router Mistakes

  • Forgetting to install react-router-dom.
  • Forgetting to import router components.
  • Using Route outside Routes.
  • Using routing components without router context.
  • Using incorrect route paths.
  • Forgetting to provide the element prop.
  • Using incorrect Link paths.

29. React Router Workflow

A basic React Router workflow is:

1. Install React Router
        ↓
2. Import router components
        ↓
3. Create page components
        ↓
4. Create BrowserRouter
        ↓
5. Define Routes
        ↓
6. Add Route components
        ↓
7. Add Link navigation
        ↓
8. Test different URLs

30. React Router Best Practices

  • Keep page components organized.
  • Use meaningful route paths.
  • Use Link for internal application navigation.
  • Keep navigation components reusable.
  • Use descriptive component names.
  • Keep route definitions easy to understand.
  • Test every important route.
  • Learn dynamic and nested routes for larger applications.
  • Handle invalid URLs appropriately.
  • Keep authentication and protected routing logic organized.
URL → Route → React Component

📌 Key Points

  • React Router is used for routing in React web applications.
  • BrowserRouter provides the routing context.
  • Routes contains route definitions.
  • Route connects a URL path with an element.
  • Link is commonly used for internal navigation.
  • React Router supports client-side routing.
  • Different URLs can display different React components.
  • Routing is an important part of larger React applications.

🧠 Quick Quiz

Question: Which React Router component is used to define an individual route?