Installing React Router
React Router is the most widely used library for client-side routing in React. Installing it adds components and hooks that map URLs to components, so /products shows a product list and /products/7 shows one product, all without full page reloads. Setting it up takes one package and a few lines that wrap your app in a router.
Installing the package
For a web project, install react-router-dom. In newer major versions the same APIs are also exported from the react-router package, but react-router-dom remains the familiar name in most tutorials and existing projects.
# inside a project created with Vite or similar
npm install react-router-dom
# check what got installed
npm ls react-router-dom
Check the major version in package.json. The examples here use the modern API from version 6 onward, which differs a lot from versions 5 and below.
Adding the router to your app
The recommended setup creates a router object with createBrowserRouter and passes it to RouterProvider at the root. Each route pairs a path with an element.
// main.jsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './pages/Home';
import Products from './pages/Products';
import NotFound from './pages/NotFound';
const router = createBrowserRouter([
{ path: '/', element: <Home />, errorElement: <NotFound /> },
{ path: '/products', element: <Products /> },
]);
createRoot(document.getElementById('root')).render(
<StrictMode>
<RouterProvider router={router} />
</StrictMode>
);
An alternative, often seen in older code and quick demos, wraps the app in <BrowserRouter> and defines routes with <Routes> and <Route> elements inside a component. Both work; the router-object style unlocks extra features such as loaders and actions for data.
Checking that it works
Run the dev server and type /products in the address bar. You should see the Products component. Then add a link with the router's Link component, such as <Link to="/products">Products</Link>, and click it; the URL changes and the new component appears without the browser reloading the page. You can confirm there was no reload because console logs and component state on unaffected parts survive the navigation.
Problems people hit after installing
The most frequent one is following a tutorial for a different version. Code using Switch, component={Home}, exact or useHistory belongs to version 5 and fails with errors like "export 'Switch' was not found". In current versions, use Routes or the router object, element={<Home />} and useNavigate.
Another is calling hooks such as useNavigate or useParams in a component that is rendered outside the router. React Router throws an error saying the hook may be used only in the context of a router. Make sure the provider wraps every component that uses router features.
A third shows up later: links work in development, but refreshing a deep URL like /products on the live site gives a 404. The server must be configured to return index.html for all app routes. Development servers like Vite do this automatically, which hides the problem until deployment.
Finally, avoid plain <a href> for internal navigation. It triggers a full reload and resets your app's state; use Link or NavLink.
Setup checklist
- Install
react-router-domand note the major version. - Create a router and render
RouterProviderat the root. - Define routes with
pathandelement. - Navigate with
Link, not plain anchors. - Plan the server fallback before deploying.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.