🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

BrowserRouter

AI & DATA SCIENCE // browserrouter

BrowserRouter is the Router implementation that uses the HTML5 History API to keep the UI synced with a clean, normal-looking URL, and is the standard router used for typical web applications.

Syntax

<BrowserRouter>
  <App />
</BrowserRouter>

Deep Dive Course

Wrapping an application in BrowserRouter enables all of React Router's routing components and hooks to function, since they all rely on routing context that only a Router component, like BrowserRouter, provides. BrowserRouter specifically uses the browser's History API to produce clean URLs, like example.com/about, as opposed to HashRouter, an older alternative that uses a URL fragment instead, like example.com/#/about — BrowserRouter requires the server to be configured to serve the same single index.html file for every possible path, since a direct visit or refresh on a route like /about is a genuine server request for that exact path, which the server needs to handle by still returning the React app.

1Understanding BrowserRouter

Wrapping an application in BrowserRouter enables all of React Router's routing components and hooks to function, since they all rely on routing context that only a Router component, like BrowserRouter, provides. BrowserRouter specifically uses the browser's History API to produce clean URLs, like example.com/about, as opposed to HashRouter, an older alternative that uses a URL fragment instead, like example.com/#/about — BrowserRouter requires the server to be configured to serve the same single index.html file for every possible path, since a direct visit or refresh on a route like /about is a genuine server request for that exact path, which the server needs to handle by still returning the React app.

💡

BrowserRouter requires your server, or hosting platform, to be configured to serve the same index.html for every route path, since a direct browser visit or refresh to a client-side route like /about is a real request the server needs to handle gracefully, not silently 404.

editor.html
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root'));
root.render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
    </Routes>
  </BrowserRouter>
);
localhost:3000

2Practical Example

Here is a real-world application of BrowserRouter showing how it is used in production React code.

editor.html
// Without BrowserRouter (or any Router) wrapping the app:
import { useNavigate } from 'react-router-dom';

function Button() {
  const navigate = useNavigate();
  return <button onClick={() => navigate('/home')}>Go</button>;
}
localhost:3000

3Best Practices

Follow these guidelines when working with BrowserRouter:

1. Wrap the application in a single BrowserRouter near the root, not multiple nested ones, so routing hooks and components throughout the tree share one consistent routing context

2. Configure your hosting/server to serve index.html for any unmatched path, so direct visits or refreshes on client-side routes don't produce a server-level 404

3. Use BrowserRouter over the older HashRouter for standard web applications, reserving HashRouter mainly for static hosting environments that genuinely can't be configured to handle arbitrary paths

⚠️

Tip: BrowserRouter requires your server, or hosting platform, to be configured to serve the same index.html for every route path, since a direct browser visit or refresh to a client-side route like /about is a real request the server needs to handle gracefully, not silently 404.

editor.html
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root'));
root.render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
    </Routes>
  </BrowserRouter>
);
localhost:3000

Examples

Example 01Basic Usage
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root'));
root.render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
    </Routes>
  </BrowserRouter>
);
Example 02Advanced Example
// Without BrowserRouter (or any Router) wrapping the app:
import { useNavigate } from 'react-router-dom';

function Button() {
  const navigate = useNavigate();
  return <button onClick={() => navigate('/home')}>Go</button>;
}

Best Practices

  • Wrap the application in a single BrowserRouter near the root, not multiple nested ones, so routing hooks and components throughout the tree share one consistent routing context
  • Configure your hosting/server to serve index.html for any unmatched path, so direct visits or refreshes on client-side routes don't produce a server-level 404
  • Use BrowserRouter over the older HashRouter for standard web applications, reserving HashRouter mainly for static hosting environments that genuinely can't be configured to handle arbitrary paths

Interview Question

Why does a direct browser refresh on a client-side route like /about require special server configuration when using BrowserRouter, but not when using HashRouter?

Hint: Think about what URL the browser actually sends to the server in each case, and whether the server needs to understand and correctly respond to that specific path.

BrowserRouter produces clean URLs like example.com/about, and when a browser directly requests that exact URL, on an initial page load or a refresh, it sends a genuine HTTP request for that specific /about path to the server, which the server needs to be explicitly configured to handle by still returning the same index.html file, letting the client-side JavaScript then take over and render the correct route — without that server-side fallback configuration, the server would look for a resource literally at /about, not find one, and return a genuine 404 error. HashRouter, by contrast, encodes the route information after a # fragment, like example.com/#/about, and browsers never actually send the fragment portion of a URL to the server as part of the request at all, so a refresh on a HashRouter-based route always requests just the base URL, example.com/, which the server can serve normally regardless of whatever route the fragment specifies, requiring no special server-side routing configuration.

Exercises

MediumPractice using BrowserRouter in a real scenario.
View Solution
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root'));
root.render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
    </Routes>
  </BrowserRouter>
);

Frequently Asked Questions

Why does a direct browser refresh on a client-side route like /about require special server configuration when using BrowserRouter, but not when using HashRouter?

BrowserRouter produces clean URLs like example.com/about, and when a browser directly requests that exact URL, on an initial page load or a refresh, it sends a genuine HTTP request for that specific /about path to the server, which the server needs to be explicitly configured to handle by still returning the same index.html file, letting the client-side JavaScript then take over and render the correct route — without that server-side fallback configuration, the server would look for a resource literally at /about, not find one, and return a genuine 404 error. HashRouter, by contrast, encodes the route information after a # fragment, like example.com/#/about, and browsers never actually send the fragment portion of a URL to the server as part of the request at all, so a refresh on a HashRouter-based route always requests just the base URL, example.com/, which the server can serve normally regardless of whatever route the fragment specifies, requiring no special server-side routing configuration.

Related Functions

routingreact-router-domroute