How to Update BrowserRouter in React
In modern React development, updating your routing setup from the
traditional BrowserRouter component to the newer
data-driven router APIs is essential for leveraging advanced features
like data loaders, actions, and HTML-over-the-wire capabilities. This
article provides a straightforward guide on how to update your React
Router configuration from the legacy BrowserRouter wrapper
to the modern createBrowserRouter and
RouterProvider architecture introduced in React Router
v6.4.
Why Upgrade to createBrowserRouter?
While the traditional <BrowserRouter> component
still works, React Router v6.4+ introduced
createBrowserRouter to support new data APIs. This update
decoupling route definition from the React render tree, enabling
features like:
- Data Loaders: Fetching data before rendering a route.
- Actions: Handling form submissions and mutations natively.
- Error Boundaries: Catching rendering or data-fetching errors gracefully at the route level.
Step 1: Migrate from BrowserRouter to RouterProvider
To update your application, you need to replace your root
<BrowserRouter> component with
createBrowserRouter and
<RouterProvider>.
The Old Way (Legacy BrowserRouter)
Previously, you wrapped your application in
BrowserRouter and defined routes inside your component
tree:
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Home from './Home';
import About from './About';
const Root = () => (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
ReactDOM.createRoot(document.getElementById('root')).render(<Root />);The New Way (RouterProvider API)
To update, define your routes in an array using
createBrowserRouter and pass that router instance to
RouterProvider at the root of your app:
import React from 'react';
import ReactDOM from 'react-dom/client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './Home';
import About from './About';
// 1. Create the router configuration
const router = createBrowserRouter([
{
path: "/",
element: <Home />,
},
{
path: "/about",
element: <About />,
},
]);
// 2. Render the RouterProvider with the router configuration
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);Step 2: Updating Programmatic Navigation
If your code previously updated routes or history programmatically
using older methods, ensure you are using the useNavigate
hook for route updates inside your components.
import { useNavigate } from 'react-router-dom';
function NavigationButton() {
const navigate = useNavigate();
const handleUpdate = () => {
// Navigates to the dashboard route programmatically
navigate('/dashboard');
};
return (
<button onClick={handleUpdate}>
Go to Dashboard
</button>
);
}By transitioning to createBrowserRouter and
RouterProvider, your React application is fully updated to
support the most efficient, modern routing paradigms available in the
React ecosystem.