BrowserRouter
BrowserRouter explains History API integration, root placement, deep-link hosting fallbacks, basename for subpath deploys, and troubleshooting router context.
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:
User clicks Link|vReact Router updates history|vURL changes without full reload|vRoutes matches the new location|vMatching React element renders
Place it at the application boundary
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.
Deep-link deployment requirement
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:
<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
BrowserRouteraround individual page components. - Expecting React Router to configure Nginx, Apache, a CDN, or a static host automatically.
- Using
BrowserRouterin 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
1How does BrowserRouter work with the History API?
BeginnerModel 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.
2Why should router hooks be used only inside router context?
BeginnerModel answer
Hooks likeuseNavigateanduseParamsread context provided by the router. Outside that tree they have no routing provider and throw or fail.
Intermediate
3Why can a nested route fail on refresh in production?
IntermediateModel answer
A refresh requests that deep URL from the server. Unless the host returns the SPAindex.htmlfor app routes, the server responds 404 even though client-side navigation worked.
Advanced
4What is the purpose of basename?
AdvancedModel answer
basenametells the router the application is served under a subpath such as/portalso 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.