React Router for Beginners: Key Features Explained
A 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 MoreThis React Router tutorial builds a tiny "team directory" app from an empty Vite project. By the end you will have a shared layout, a home page, a list of people, a detail page for each person read from the URL, an active menu, a redirect after a form, and a not-found page. That covers most of what everyday apps need from React Router.
We use Declarative Mode, the simplest of React Router's three modes, with React Router 8.5.3. If you have not installed it yet, follow How to Download and Install React Router first, then come back.
npm create vite@latest team-directory -- --template react
cd team-directory
npm i
npm i react-router
Open src/main.jsx and wrap the app in BrowserRouter. This component connects React Router to the browser's address bar and history:
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router";
import App from "./App";
createRoot(document.getElementById("root")).render(
<BrowserRouter>
<App />
</BrowserRouter>
);
You only do this once, at the very top. Every hook and link in the rest of the app depends on it.
Before writing components, decide which URLs the app will have. It keeps the route tree tidy:
/ shows a welcome page./people lists everyone./people/:personId shows one person, for example /people/ada./people/new shows a form to add someone.Almost every app has parts that never change between pages, such as a header and a menu. In React Router, those go in a layout route. The layout renders <Outlet /> wherever the current child page should appear.
// src/Layout.jsx
import { NavLink, Outlet } from "react-router";
export default function Layout() {
return (
<div className="shell">
<header>
<strong>Team Directory</strong>
<nav>
<NavLink to="/" end>Home</NavLink>
<NavLink to="/people">People</NavLink>
</nav>
</header>
<main>
<Outlet />
</main>
</div>
);
}
Notice NavLink instead of Link. It behaves the same but adds an active class when its URL matches, so you can highlight the current menu item with plain CSS: nav a.active { font-weight: 700; }. The end prop on the Home link stops it from being "active" on every page, since every URL starts with /.
Now replace src/App.jsx with the route tree. Child routes are nested inside the layout route, and their paths are relative to the parent:
import { Routes, Route } from "react-router";
import Layout from "./Layout";
import Home from "./Home";
import People from "./People";
import Person from "./Person";
import NewPerson from "./NewPerson";
import NotFound from "./NotFound";
export default function App() {
return (
<Routes>
<Route element={<Layout />}>
<Route index element={<Home />} />
<Route path="people" element={<People />} />
<Route path="people/new" element={<NewPerson />} />
<Route path="people/:personId" element={<Person />} />
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
);
}
Three details worth knowing:
index marks the default child, shown at the parent's own URL (here /).people/new beats people/:personId for the URL /people/new because a static segment scores higher than a dynamic one.Keep the data simple for now, in src/data.js:
export const people = [
{ id: "ada", name: "Ada Lovelace", role: "Engineer" },
{ id: "grace", name: "Grace Hopper", role: "Architect" },
{ id: "alan", name: "Alan Turing", role: "Researcher" },
];
The list page links to each person with Link. Relative paths work here: to={p.id} inside /people resolves to /people/ada.
// src/People.jsx
import { Link } from "react-router";
import { people } from "./data";
export default function People() {
return (
<>
<h1>People</h1>
<ul>
{people.map((p) => (
<li key={p.id}><Link to={p.id}>{p.name}</Link></li>
))}
</ul>
<Link to="new">Add a person</Link>
</>
);
}
The detail page needs to know which person to show. The :personId segment in the route path becomes a key in the object returned by useParams:
// src/Person.jsx
import { useParams, Link } from "react-router";
import { people } from "./data";
export default function Person() {
const { personId } = useParams();
const person = people.find((p) => p.id === personId);
if (!person) return <p>No one called "{personId}" works here.</p>;
return (
<article>
<h1>{person.name}</h1>
<p>{person.role}</p>
<Link to=".." relative="path">Back to people</Link>
</article>
);
}
Params are always strings. If your IDs are numbers, convert them with Number(personId) before comparing. The relative="path" option makes .. go up one URL segment (from /people/ada to /people) rather than up one route level.
Links cover clicks. For everything else, such as moving the user after a form is saved, use the useNavigate hook:
// src/NewPerson.jsx
import { useNavigate } from "react-router";
import { people } from "./data";
export default function NewPerson() {
const navigate = useNavigate();
function handleSubmit(e) {
e.preventDefault();
const name = new FormData(e.currentTarget).get("name");
const id = String(name).toLowerCase().replace(/\s+/g, "-");
people.push({ id, name, role: "New hire" });
navigate(`/people/${id}`, { replace: true });
}
return (
<form onSubmit={handleSubmit}>
<label>Name <input name="name" required /></label>
<button>Save</button>
</form>
);
}
{ replace: true } replaces the form page in history, so pressing Back after saving does not return the user to an empty form. Call navigate(-1) to go back one step, just like the browser's Back button.
The path="*" route catches anything that no other route matched. Give it a helpful message and a way home:
// src/NotFound.jsx
import { Link } from "react-router";
export default function NotFound() {
return (
<>
<h1>Page not found</h1>
<Link to="/">Go home</Link>
</>
);
}
Because it sits inside the layout route, the header and menu still show on the not-found page.
Start the dev server with npm run dev and try these checks:
/people/grace straight into the address bar: the detail page loads directly./nowhere: the not-found page shows inside the layout.You now know the core pieces: BrowserRouter, Routes, Route, Link, NavLink, Outlet, useParams and useNavigate. Next steps worth exploring:
useSearchParams.loader functions so pages render with their data ready.React Router key features explained gives an overview of all of these, and Best uses for React Router shows how real apps combine them. If something in this walkthrough throws an error, check common React Router errors and fixes.
Get React Router 8.5.3: one npm command, or the verified package download.
DownloadRelated articles
A 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 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