Skip to main content

createBrowserRouter

Create a new data router that manages the application path via history.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

The routes 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 initialized DataRouter 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

Use createBrowserRouter 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 createHashRouter if your hosting doesn’t support client-side routing fallbacks
  • Testing environment: Use createMemoryRouter for 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.pushState and history.replaceState
  • Server configuration to handle client-side routes (serve index.html for all routes)

Server Configuration

Since createBrowserRouter uses clean URLs, your server must be configured to serve your app’s entry point for all routes:

Nginx

Apache

Express.js