createBrowserRouter vs BrowserRouter
What you'll learn: Why React Router's new data APIs require createBrowserRouter and what changes in your setup.
Two Routing Philosophies
In React Router v6.4+, there are two ways to set up routing, and they're not interchangeable:
BrowserRouter is the classic component-based approach. You wrap your app, declare <Routes> and <Route> components in JSX, and everything happens at render time. This is what you've been using until now.
createBrowserRouter is the newer function-based approach that creates a router object before rendering. Instead of JSX, you pass a route configuration array, and the router is then provided via RouterProvider.
Why the Split?
The data router paradigm (loaders, actions, error boundaries) requires the router to know your entire route tree before React starts rendering. This allows the router to:
- Call loaders in parallel before rendering components
- Coordinate mutations with actions
- Handle errors at route boundaries
- Enable prefetching and navigation optimizations
BrowserRouter can't do this because routes are discovered during render. createBrowserRouter builds the full route map upfront, unlocking these data features.
What Changes in Setup
// Old way: BrowserRouter (no data features)
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
// New way: createBrowserRouter (with data features)
const router = createBrowserRouter([
{ path: "/", element: <Home />, loader: homeLoader }
]);
<RouterProvider router={router} />
The route structure moves from JSX to a configuration array. You lose the flexibility of conditional routes at render time, but gain access to loaders, actions, and error handling.
Key Takeaway: createBrowserRouter is required for data APIs because it builds the route tree before rendering, enabling loaders and actions to run before components mount—something BrowserRouter's runtime discovery cannot support.