HashRouter
A declarative<Router> that stores the location in the hash portion of the URL (window.location.hash) so it is not sent to the server.
This is useful for situations where you cannot configure your server to handle URLs properly, such as when deploying to static file hosts.
Import
Type Declaration
Props
The base URL for all locations. If your app is served from a subdirectory, set this to the subdirectory path.
Your route configuration, typically
<Routes> and <Route> elements.Control whether router state updates are wrapped in
React.startTransition.undefined(default): All state updates are wrapped instartTransitiontrue: Link/Form navigations and all state updates usestartTransitionfalse: Router doesn’t usestartTransitionat all
Override the default
window object. Useful for testing or iframe scenarios.Examples
Basic Usage
- Home:
http://example.com/#/ - About:
http://example.com/#/about
With Basename
- Home:
http://example.com/#/app/ - Settings:
http://example.com/#/app/settings
Custom Window (Testing)
When to Use
UseHashRouter when:
- You’re deploying to static file hosts (GitHub Pages, AWS S3, Netlify without rewrites)
- You cannot configure your server to handle client-side routing
- You need all routing to happen purely on the client
For modern deployments, prefer
<BrowserRouter> with proper server configuration, or use createHashRouter() for the Data Router API.Comparison with BrowserRouter
| Feature | HashRouter | BrowserRouter |
|---|---|---|
| URL Format | example.com/#/path | example.com/path |
| Server Config | Not required | Required |
| SSR Support | No | Yes |
| SEO | Limited | Full |
| Clean URLs | No | Yes |
| Static Hosting | ✅ Works great | Requires rewrites |
See Also
<BrowserRouter>- For clean URLs with server supportcreateHashRouter()- Hash routing with Data Router API- Static Deployment Guide