How to Download and Install React Router (Step by Step)
Every 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 MoreFeature lists only go so far. The best way to understand React Router is to see how real applications put it to work. Below are six patterns you will find in production apps, from admin dashboards to online stores, each with the route setup and the handful of lines that make it work. The examples use React Router 8 and plain JSX, and they are short on purpose so you can adapt them.
New to the basics? Read How to Use React Router: A Beginner's Walkthrough first. It explains Routes, Outlet and useParams step by step.
The need: a sidebar and top bar that never reload, with reports, users and settings swapping in the main area. Each section should have its own URL so people can bookmark "Users" or share a link to "Reports".
The pattern: a layout route renders the shell and an <Outlet />; every section is a child route.
<Route path="admin" element={<AdminShell />}>
<Route index element={<Overview />} />
<Route path="reports" element={<Reports />} />
<Route path="users" element={<Users />} />
<Route path="users/:userId" element={<UserDetail />} />
<Route path="settings" element={<Settings />} />
</Route>
Inside AdminShell, the sidebar uses NavLink so the current section is highlighted automatically. Because the shell stays mounted, scroll position in the sidebar, open menus and any WebSocket connection survive navigation. In Data or Framework Mode, each section's loader fetches its data in parallel when the route matches.
The need: clean, readable URLs like /shop/shoes/sku-1042 that search engines can index and customers can share.
The pattern: two dynamic segments.
<Route path="shop">
<Route index element={<AllCategories />} />
<Route path=":category" element={<Category />} />
<Route path=":category/:sku" element={<Product />} />
</Route>
function Product() {
const { category, sku } = useParams();
// fetch product by sku, show breadcrumbs using category
}
For SEO, stores usually pick Framework Mode so product pages are rendered on the server or pre-rendered at build time, with a meta export that sets the title and description for each product.
The need: anything under /account must only be visible to signed-in users. Guests should land on /login and come back where they started after signing in.
The pattern in Declarative Mode: a guard layout that either renders the outlet or redirects.
import { Navigate, Outlet, useLocation } from "react-router";
function RequireAuth() {
const user = useUser(); // your auth hook
const location = useLocation();
if (!user) {
return <Navigate to="/login" replace state={{ from: location.pathname }} />;
}
return <Outlet />;
}
<Route element={<RequireAuth />}>
<Route path="account" element={<Account />} />
<Route path="orders" element={<Orders />} />
</Route>
After login, read location.state.from and call navigate(from, { replace: true }).
In Data or Framework Mode the cleaner place for this check is middleware or a loader, which runs before any protected data is fetched: if there is no session, throw redirect("/login"). Remember that a client-side guard only hides UI; your API must still check permissions on the server.
The need: a job board or product search where users combine filters. Refreshing the page or sharing the link must keep the same results.
The pattern: store every filter in the query string with useSearchParams instead of component state.
function Jobs() {
const [params, setParams] = useSearchParams();
const remote = params.get("remote") === "1";
const sort = params.get("sort") ?? "newest";
function toggleRemote() {
setParams((prev) => {
remote ? prev.delete("remote") : prev.set("remote", "1");
return prev;
});
}
// fetch jobs using remote and sort ...
}
Now /jobs?remote=1&sort=salary is a complete, shareable view. The Back button steps through filter changes too, which users expect on search pages. If you do not want every keystroke in a search box to create a history entry, pass { replace: true } as the second argument to setParams.
The need: fast pages with good SEO and readable slugs like /blog/react-router-tips.
The pattern: Framework Mode with pre-rendering. Routes are defined in app/routes.ts, and react-router.config.ts lists the URLs to render to static HTML at build time:
// app/routes.ts
import { index, route } from "@react-router/dev/routes";
export default [
index("./routes/home.tsx"),
route("blog/:slug", "./routes/post.tsx"),
];
// react-router.config.ts
export default {
async prerender() {
const slugs = await getAllSlugs();
return ["/", ...slugs.map((s) => `/blog/${s}`)];
},
};
Each post loads instantly as static HTML, then becomes a fully interactive React app after hydration. Links between posts are client-side navigations, so moving around the site feels immediate.
The need: a checkout or onboarding flow with several steps, where Back works and each step can be linked to directly.
The pattern: one child route per step inside a wizard layout that holds shared state and a progress bar.
<Route path="checkout" element={<CheckoutLayout />}>
<Route index element={<Navigate to="shipping" replace />} />
<Route path="shipping" element={<Shipping />} />
<Route path="payment" element={<Payment />} />
<Route path="review" element={<Review />} />
</Route>
CheckoutLayout keeps the order data (in context or the Outlet's context prop) so it survives step changes, and highlights the current step with NavLink. Each step's "Continue" button calls navigate("../payment").
<Outlet />, or child pages appear blank.path="users", not path="/users", unless you really mean to break out of the parent.| Use case | Recommended mode | Why |
|---|---|---|
| Internal dashboard | Data | Loaders per panel, no SEO needs |
| Online store | Framework | SSR for SEO and fast first load |
| Protected app | Data or Framework | Middleware runs before data loads |
| Search page | Any | useSearchParams works everywhere |
| Blog or docs | Framework | Pre-rendering to static HTML |
| Wizard in an existing app | Declarative | Smallest change to the codebase |
Want the reasoning behind each feature used here? React Router key features explained covers them one by one. If an example throws an error in your project, common React Router errors and fixes has the solution for the usual suspects.
Get React Router 8.5.3: one npm command, or the verified package download.
DownloadRelated articles
Every 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 MoreBuild a small multi-page React app from scratch and learn Routes, Link, NavLink, Outlet, useParams and useNavigate along the way.
Read MoreA plain-English tour of what React Router can do, from nested layouts and URL params to loaders, actions, type-safe routes and server rendering.
Read More