How to Use React Router: A Beginner's Walkthrough
Build a small multi-page React app from scratch and learn Routes, Link, NavLink, Outlet, useParams and useNavigate along the way.
Read MoreReact Router has grown from a simple URL-to-component matcher into a full routing toolkit, and in version 7 it absorbed the Remix framework as well. That is great for power, but it can make the feature list feel overwhelming when you are new. This article walks through the React Router features you will actually use, grouped from "day one" to "when your app grows", with a short example of each.
Everything here applies to React Router 8.5.3. If you want to follow along in code, install it with our step-by-step installation walkthrough.
You describe which component belongs to which URL, and React Router handles the rest. There is no manual if (location === ...) logic.
<Routes>
<Route path="/" element={<Home />} />
<Route path="pricing" element={<Pricing />} />
</Routes>
<Link> renders a real <a href>, so it works with keyboard navigation, screen readers, right-click and "open in new tab". When clicked normally, React Router intercepts it and updates the page without asking the server for new HTML. Pages feel instant, and the browser's Back and Forward buttons keep working.
<NavLink> is a Link that knows whether it matches the current URL. It adds an active class and aria-current="page" automatically, and accepts functions for className and style:
<NavLink to="/inbox" className={({ isActive }) => isActive ? "tab on" : "tab"}>
Inbox
</NavLink>
It also exposes isPending, which is handy for showing a subtle loading state on the link the user just clicked.
The useNavigate hook returns a function you can call after any event: a login, a saved form, a timer. Use navigate("/home") to go somewhere, navigate(-1) to go back, and { replace: true } when the current page should not stay in history.
This is the feature that makes React Router scale. Routes can contain routes, and each parent decides where its child appears using <Outlet />. A settings area with its own sidebar looks like this:
<Route path="settings" element={<SettingsLayout />}>
<Route index element={<Profile />} />
<Route path="billing" element={<Billing />} />
<Route path="security" element={<Security />} />
</Route>
The sidebar inside SettingsLayout stays mounted while only the inner panel changes. Routes without a path ("layout routes") let you share a layout between URLs that have nothing in common, such as wrapping all logged-in pages in one shell.
/projects/:projectId matches /projects/42 and gives you projectId = "42" through useParams./:lang?/about matches both /about and /fr/about./files/* matches /files/a/b/c.txt and puts a/b/c.txt in params["*"].React Router ranks all paths so the most specific match wins, regardless of the order you wrote them in.
Filters, sorting and pagination belong in the query string so they survive a refresh and can be shared. useSearchParams reads and writes them like React state:
const [params, setParams] = useSearchParams();
const sort = params.get("sort") ?? "newest";
setParams({ sort: "price" }); // URL becomes ?sort=price
A loader is a function attached to a route that fetches the data that route needs. React Router calls it before rendering, in parallel for every matched route, so nested pages do not create a waterfall of spinners.
{
path: "projects/:projectId",
loader: async ({ params }) => fetchProject(params.projectId),
Component: Project,
}
Inside the component, useLoaderData() returns the result.
Actions handle data changes. A <Form method="post"> submits to the route's action, and after the action finishes React Router automatically re-runs the loaders on the page so the UI shows fresh data. No manual cache juggling.
useNavigation() tells you when a navigation or submission is in progress, and useFetcher() lets you load or submit data without changing the URL, which is perfect for "like" buttons and inline edits.
In v8, middleware is always on. It runs before loaders and actions, which makes it the natural place for authentication checks, logging and shared context.
Each route can export an ErrorBoundary. If its loader, action or component throws, only that part of the page is replaced with the error UI, and the rest of the layout keeps working.
Two small features make apps feel polished. <ScrollRestoration /> (Data and Framework modes) remembers the scroll position of each page, so pressing Back returns users to where they were in a long list instead of the top. useBlocker lets you stop a navigation when a form has unsaved changes and ask the user to confirm before leaving. Both replace code that teams used to write by hand, and both respect the browser's normal behaviour where they can.
Framework Mode wraps everything above in a Vite plugin and adds:
loader, action, meta, ErrorBoundary and the component.href() helper that catches links to routes that do not exist.| Feature | Declarative | Data | Framework |
|---|---|---|---|
| Routes, Link, NavLink, params | Yes | Yes | Yes |
| Loaders, actions, fetchers | No | Yes | Yes |
| Middleware | No | Yes | Yes |
| Type-safe route modules | No | No | Yes |
| SSR and pre-rendering | No | Manual | Built in |
| Setup effort | Lowest | Low | Starter template |
The modes are additive. You can start in Declarative Mode and move up later without throwing away your components.
Learn them in this order: routes, Link, NavLink, nested routes with Outlet, useParams, useNavigate. That set is enough to build most single-page apps, and it is exactly what our beginner's walkthrough covers. Then add search params, and move to loaders and actions once you notice data fetching spreading across your components.
For real projects that use these features together, see Best uses for React Router (real examples).
Get React Router 8.5.3: one npm command, or the verified package download.
DownloadRelated articles
Build a small multi-page React app from scratch and learn Routes, Link, NavLink, Outlet, useParams and useNavigate along the way.
Read MoreEvery way to get React Router running: one npm command, a new Framework Mode app, or an offline install from the downloaded package, with checks at each step.
Read MoreExactly what React Router 8 needs to run, how that compares with v7 and v6, and which bundlers, browsers, operating systems and hosts work.
Read More