← React Beginner Questions - Set 3 Q22

What is the difference between BrowserRouter and HashRouter?

React Beginner Questions - Set 3

Same API, different URL shape. The hash variant exists for hosting where you cannot rewrite server paths.

Example

// BrowserRouter -> https://app.example.com/users/7
import { BrowserRouter } from 'react-router-dom';
<BrowserRouter><App /></BrowserRouter>

// HashRouter -> https://app.example.com/#/users/7
import { HashRouter } from 'react-router-dom';
<HashRouter><App /></HashRouter>

// Deep link /users/7 needs the server to answer with index.html,
// otherwise the request 404s before React ever loads.
# nginx
location / { try_files $uri /index.html; }

What to notice

← Back to the question list