createBrowserRouter
Create a new data router that manages the application path viahistory.pushState and history.replaceState.
This is the recommended router for all React Router web applications and is used for data-driven applications with loaders and actions.
Function Signature
Route Configuration
Theroutes parameter is an array of route objects that define your application’s routing structure. Each route object can be either an index route or a non-index route.
Route Object Properties
Options Parameter
Return Type
Returns an initializedDataRouter instance to be passed to <RouterProvider>.
Examples
Basic Usage
With Basename
With Loaders and Actions
With Lazy Loading
With Error Boundaries
With Hydration Data (SSR)
With Context Provider
With Lazy Route Discovery (Fog of War)
Use Cases
When to Use createBrowserRouter
UsecreateBrowserRouter when:
- Building a modern web application with data loading requirements
- You need loaders and actions for data fetching and mutations
- You want the best performance with automatic code splitting via
lazy - You need advanced features like middleware, error boundaries, and hydration
- You’re building a single-page application (SPA) with clean URLs
- Server-side rendering (SSR) support is required
- You want to use React Router’s data APIs
When NOT to Use createBrowserRouter
Consider alternatives when:- Static hosting without server support: Use
createHashRouterif your hosting doesn’t support client-side routing fallbacks - Testing environment: Use
createMemoryRouterfor unit/integration tests - Simple declarative routing: Use
<BrowserRouter>if you don’t need data APIs - Server environment: This is a client-only router; use server-specific routers for SSR data handling
Browser Support
createBrowserRouter requires:
- Modern browsers supporting the History API
history.pushStateandhistory.replaceState- Server configuration to handle client-side routes (serve
index.htmlfor all routes)
Server Configuration
SincecreateBrowserRouter uses clean URLs, your server must be configured to serve your app’s entry point for all routes:
Nginx
Apache
Express.js
Related
<RouterProvider>- Component to render the routercreateHashRouter- Hash-based routing alternativecreateMemoryRouter- In-memory routing for testingloader- Route data loadingaction- Route data mutations