BrowserRouter BrowserRouter Routing Container

Last updated: 2026-11-02

BrowserRouter BrowserRouter Routing Container

BrowserRouter is the routing container of react-router-dom: it watches the address and provides the current location to the whole component tree through context. It must wrap the outermost layer of the app so that Routes, Route, Link and the other routing APIs have a router to talk to. Every navigation then becomes a history push instead of a full page reload, which is what makes a single-page app feel instant to the user.

GroupRouting & State Mgmt
KindComponent
SinceReact Router 6.4

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

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

createRoot(document.getElementById('root')).render(
  <BrowserRouter>            {/* listens to the History API and provides the location context */}
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/about" element={<About />} />
    </Routes>
  </BrowserRouter>
)

⚙️ Parameters

childrenThe application content; every component inside the container can read the routing context.
basenameOptional. A prefix added to all paths when the app is deployed under a sub-path, e.g. basename='/app'.

Returns:Nothing (it is a component). It renders children and broadcasts the current location and the navigation methods to the whole subtree.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Clicking a nav button switches the page and the current path is wrapped in brackets - exactly what BrowserRouter does internally: the path changes, the context updates and the matching Route renders (simulated demo).

💡 Key Points

ℹ️ Info BrowserRouter uses real URLs (/about), so a refresh needs the server to fall back to index.html for every path; HashRouter with #/about has no such requirement.
💡 Tip Since v6.4 a new project can use createBrowserRouter + RouterProvider directly: the style is more functional and it also supports loader data prefetching.
⚠️ Warning There must be exactly one BrowserRouter instance and it has to wrap every component that uses the routing APIs, so it belongs at the very top of the tree.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q How do I choose between BrowserRouter and HashRouter?
A Use BrowserRouter whenever the server can be configured with the fallback (clean URLs, better SEO); use HashRouter for static hosting where the server cannot be changed.
Q I get a 404 after refreshing. What now?
A The /about of BrowserRouter is a front-end path and does not exist on the server; rewrite every path to index.html (try_files on Nginx, supported by default on Vercel and Netlify).
Q What is createBrowserRouter?
A The data router style introduced in v6.4: you describe the routes in a config object (with loaders and actions) and render them with RouterProvider. It is a superset of BrowserRouter plus Routes.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏