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

    BrowserRouter

    BrowserRouter explains History API integration, root placement, deep-link hosting fallbacks, basename for subpath deploys, and troubleshooting router context.

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

    Course: React — Routing & API Integration Lesson: 02

    Learning objectives

    Understand how BrowserRouter integrates React Router with browser history, where it belongs in the component tree, and what deployment behavior is required for deep links.

    What BrowserRouter does

    BrowserRouter uses the browser's History API to keep the UI synchronized with the URL. Navigation can update the address bar and render a new route without downloading a completely new HTML document for every transition.

    A simplified flow:

    text
    User clicks Link
    |
    v
    React Router updates history
    |
    v
    URL changes without full reload
    |
    v
    Routes matches the new location
    |
    v
    Matching React element renders

    Place it at the application boundary

    javascript
    import { StrictMode } from "react";
    import { createRoot } from "react-dom/client";
    import { BrowserRouter } from "react-router-dom";
    import App from "./App";
    createRoot(document.getElementById("root")).render(
    <StrictMode>
    <BrowserRouter>
    <App />
    </BrowserRouter>
    </StrictMode>
    );

    This makes routing context available to descendants. Components using router hooks such as useNavigate, useLocation, and useParams must render within the router context.

    Avoid wrapping every page separately. A single application-level router gives the application one coherent navigation history.

    Browser history behavior

    The browser maintains a history stack. React Router integrates with it so that:

    • push-style navigation adds a history entry.
    • replace-style navigation replaces the current entry.
    • Back and forward buttons restore previous locations.
    • The address bar reflects the current route.

    For example, navigating from /dashboard to /orders normally creates a new history entry. Redirecting after login may replace /login so the Back button does not immediately return the user to the sign-in screen.

    A common production issue occurs when /orders/123 works after navigating from the home page but returns a server 404 when refreshed. The browser requests that URL directly from the web server. The server must be configured to return the SPA's index.html for application routes, while still serving real static assets normally.

    This is a hosting configuration responsibility, not a React component fix.

    Basename and deployment under a subpath

    If an application is hosted below a path such as /portal, router configuration may need a basename:

    javascript
    <BrowserRouter basename="/portal">
    <App />
    </BrowserRouter>

    The hosting environment and asset paths must agree with this deployment prefix.

    Troubleshooting checklist

    • Confirm there is one intended top-level router.
    • Confirm router hooks are not called outside router context.
    • Refresh a nested route in the production-like environment.
    • Verify server fallback/rewrite rules.
    • Check whether a reverse proxy strips or adds a path prefix.
    • Inspect the browser console for route-context errors.

    Common mistakes

    • Adding BrowserRouter around individual page components.
    • Expecting React Router to configure Nginx, Apache, a CDN, or a static host automatically.
    • Using BrowserRouter in a non-browser rendering environment without considering the appropriate router for that environment.
    • Forgetting a basename when deployed under a subdirectory.

    Best practices

    Keep router ownership centralized, test deep links before release, and document the hosting rewrite requirement in deployment instructions.

    Interview questions & answers

    Interview preparation

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

    Beginner

    2
    1. 1How does BrowserRouter work with the History API?
      Beginner

      Model answer

      It keeps the UI synchronized with the URL by using push/replace history entries so navigation can change the address bar and render a matching route without downloading a new HTML document each time.

    2. 2Why should router hooks be used only inside router context?
      Beginner

      Model answer

      Hooks like useNavigate and useParams read context provided by the router. Outside that tree they have no routing provider and throw or fail.

    Intermediate

    1
    1. 3Why can a nested route fail on refresh in production?
      Intermediate

      Model answer

      A refresh requests that deep URL from the server. Unless the host returns the SPA index.html for app routes, the server responds 404 even though client-side navigation worked.

    Advanced

    1
    1. 4What is the purpose of basename?
      Advanced

      Model answer

      basename tells the router the application is served under a subpath such as /portal so route matching and links stay aligned with the deployment prefix.

    Practice

    Configure BrowserRouter at the root, add a /reports route, and test direct navigation, refresh, Back, and Forward. Then simulate deployment under /portal.

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