React Tutorial 0/120 lessons ~6 min read Lesson 46

    React Router Introduction

    React Router Introduction covers client-side routing, core React Router building blocks, feature-oriented route organization, and why routing is not authorization.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Course: React — Routing & API Integration Lesson: 01

    Learning objectives

    By the end of this lesson, you will be able to explain client-side routing, identify when an SPA router is useful, understand the core React Router building blocks, and organize routes around application features.

    Why routing matters

    A React application often behaves like a collection of screens: dashboard, products, orders, profile, sign-in, and administration. Without a routing solution, developers may manually inspect window.location, conditionally render pages, and implement browser-history behavior themselves. That approach becomes difficult to maintain as the application grows.

    Client-side routing maps a URL to a React view without requiring a full document reload for every navigation. The browser URL remains meaningful and shareable, while React updates the displayed route.

    Typical route examples:

    • / — landing page
    • /products — product catalogue
    • /products/42 — product details
    • /account/profile — profile page
    • /login — sign-in page

    Install and understand the core API

    Install React Router in a browser application:

    bash
    npm install react-router-dom

    The core concepts used throughout this section are:

    • BrowserRouter: connects routing to the browser History API.
    • Routes: selects the best matching route branch.
    • Route: associates a URL pattern with an element.
    • Link and NavLink: navigate without a full page reload.
    • useNavigate: navigate programmatically.
    • useParams: read dynamic path segments.
    • useLocation: inspect the current location.

    A first route configuration

    javascript
    import { BrowserRouter, Routes, Route } from "react-router-dom";
    function HomePage() {
    return <h1>Welcome</h1>;
    }
    function ProductsPage() {
    return <h1>Products</h1>;
    }
    function App() {
    return (
    <BrowserRouter>
    <Routes>
    <Route path="/" element={<HomePage />} />
    <Route path="/products" element={<ProductsPage />} />
    </Routes>
    </BrowserRouter>
    );
    }
    export default App;

    BrowserRouter should normally be placed once near the application root. Routes evaluates its child route definitions and renders the matching route element.

    Routing is not authorization

    A route determines which UI is rendered for a URL. It does not secure backend data. A user can manually enter a URL, modify browser state, or call an API outside the application. Sensitive operations must be authorized by the server on every request.

    Practical SaaS example

    For a small order-management SaaS, begin with route groups such as public pages (/login, /register), authenticated workspace pages (/dashboard, /orders), and resource pages (/orders/:orderId). Keep route definitions readable and group related screens by feature as the application expands.

    Common mistakes

    • Creating multiple independent BrowserRouter instances.
    • Using ordinary <a href> for every internal navigation, causing full document loads.
    • Treating a hidden navigation link as a security control.
    • Defining route paths in many unrelated components without a clear ownership strategy.
    • Forgetting that a production server must serve the SPA entry point for deep links.

    Best practices

    • Keep the router close to the application root.
    • Use Link or NavLink for internal navigation.
    • Organize routes by product area or feature.
    • Use meaningful, stable URL structures.
    • Add a not-found route.
    • Test direct URL entry and browser back/forward behavior.

    Interview questions & answers

    Interview preparation

    5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.

    Beginner

    2
    1. 1What is client-side routing?
      Beginner

      Model answer

      Client-side routing maps a browser URL to a React view without a full document reload for every navigation, while keeping the URL shareable and meaningful.

    2. 2How is routing different from authorization?
      Beginner

      Model answer

      Routing chooses which UI to render for a URL.

      Authorization decides what a caller may do with data and must be enforced by the backend on every sensitive request.

    Intermediate

    2
    1. 3What problem does React Router solve in an SPA?
      Intermediate

      Model answer

      It owns History API integration, route matching, and navigation helpers so teams do not hand-roll window.location checks and history behavior as the app grows.
    2. 4Why should internal navigation generally use Link rather than a normal anchor?
      Intermediate

      Model answer

      Link updates the client-side location without a full document load, preserving SPA state and avoiding unnecessary reloads that a normal <a href> would cause.

    Advanced

    1
    1. 5What is the responsibility of BrowserRouter?
      Advanced

      Model answer

      BrowserRouter connects React Router to the browser History API and provides routing context for descendants that use router hooks and navigation components.

    Practice

    Create routes for Home, Dashboard, Products, Orders, and Not Found. Verify that each URL works when entered directly into the address bar and when reached through in-app navigation.

    Ready to mark this lesson complete?Track your journey across the entire course.