Full-Stack Development · Tutorial 7 of 9
React Router: pages and navigation in a React app
By the FUTEAC Team · 16 November 2026 · 13 min read · Beginner
In short
React Router turns one React app into many pages by matching the browser URL to a component. List your routes once, link between them with Link, read a user id from the URL with useParams, and share a layout with Outlet. The demo is a small users app with four pages, and every route in it was built and rendered.
- A route is a pair: a URL pattern and the component to show for it. The router matches the URL and renders the component.
Linkchanges the page without reloading it. A plain<a>tag reloads the whole app and throws away its state.- A layout route wraps its child routes in shared markup, and
Outletmarks where the child page goes. - A dynamic segment like
:idturns one route into a page per user, per post, or per product. - This tutorial feeds the React from Scratch course (/course/react-from-scratch) and the React Router (/skill/react-router) skill.
Before you start
What is routing, and why does a React app need it?
A React app without routing is one long page. Everything lives in a single component tree, and the only way to show something else is to hide one part and show another. That works for a demo, and it falls apart for a real app, because the URL never changes: users cannot bookmark a page, share a link to it, or press the back button to return to it.
Routing fixes that by giving each page an address. Think of a house. Without routing, the house has one room and no doors. Routing adds rooms and doors, and the URL is the address on each door. is the front door, /about is the study, /users/2 is Bilal's room. The router reads the address, opens the right door, and shows the room behind it. The address bar is part of the app, not decoration: it is how users find their way back.
How do you set up the demo app?
You need a Vite React app and the router package. Run these commands in your terminal:
npm create vite@latest my-router-app -- --template react
cd my-router-app
npm install
npm install react-router
npm run devOpen the address Vite prints, usually http://localhost:5173. You should see the Vite starter page. The versions installed for this tutorial were Vite 8.3.8, React 19.3.1, and React Router 8.5.1.
The starter already has src/main.jsx, which renders the App component. Leave it alone. All the work in this tutorial goes in one file, src/App.jsx. Replace its contents step by step as you follow along.
How do you add your first two routes?
Open src/App.jsx and replace everything in it with this:
import { BrowserRouter, Routes, Route, Link } from "react-router";
function Home() {
return <h1>Welcome to the router demo</h1>;
}
function About() {
return <h1>About this app</h1>;
}
export default function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}Four pieces do the work. BrowserRouter wraps the app and watches the URL. Routes holds the list of routes. Each Route pairs a path with the component to show, through element. Link makes a clickable link that changes the page without reloading it.
Save the file and click About in the browser. The heading changes, and the address bar shows /about. Press the back button. The heading changes back, and the address bar shows . That is routing: the URL and the page move together.
The wrong turn: a layout with no `Outlet`
Here is the wrong turn almost everyone makes once. You write the layout, you nest the routes, you save, and the page shows the nav bar and nothing else. No error in the console, no red text, just an empty page under the nav.
The cause is a missing Outlet. This layout looks finished, but it is not:
function Layout() {
return (
<div>
<nav>
<Link to="/">Home</Link> | <Link to="/users">Users</Link>
</nav>
<main>
{/* nothing here: the child route has nowhere to go */}
</main>
</div>
);
}The router still matches the URL, and it still picks the right child component. Then it looks for the Outlet to render it into, finds none, and renders nothing. The nav stays because it is plain markup in the layout. The page stays empty because the child has no slot to appear in.
The fix is one line inside <main>:
<Outlet />We checked this the hard way: rendering the app with this layout shows the nav and an empty page, and adding Outlet brings the child page back. Think of it as a door frame with the doorway bricked up. The hallway is there, the rooms are there, but nobody can walk through. Outlet is the doorway.
How do you make one page per user?
A users page needs one route, not one route per user. The trick is a dynamic segment: a part of the path that changes. Add a users array and two components to src/App.jsx:
const users = [
{ id: "1", name: "Ayesha", role: "Frontend developer" },
{ id: "2", name: "Bilal", role: "Backend developer" },
];
function Users() {
return (
<div>
<h1>Users</h1>
<ul>
{users.map((user) => (
<li key={user.id}>
<Link to={`/users/${user.id}`}>{user.name}</Link>
</li>
))}
</ul>
</div>
);
}
function UserDetail() {
const { id } = useParams();
const user = users.find((u) => u.id === id);
if (!user) {
return <h1>User not found</h1>;
}
return (
<div>
<h1>{user.name}</h1>
<p>Role: {user.role}</p>
<p>User ID: {user.id}</p>
</div>
);
}Add the two routes inside the layout, under the About route:
<Route path="users" element={<Users />} />
<Route path="users/:id" element={<UserDetail />} />useParams reads the changing part of the URL. On /users/2, id is "2", and the component looks up the matching user. Open /users and click Bilal. The page shows his name and role, and the address bar shows /users/2. Open /users/99, an id with no user, and the page shows "User not found" instead of crashing. That guard matters: a user can type any address, and your page has to cope with an id that matches nothing.
One detail: id is always a string, even when it looks like a number. The array stores ids as strings for exactly that reason, so "2" === "2" matches. If your ids are numbers, convert first with Number(id).
What happens when no route matches?
Open an address that has no route, like /nope. The page goes blank. No error, no message, just nothing. The router found no match, so it rendered nothing at all.
The fix is a catch-all route, a route whose path is *, meaning "anything else". Add it as the last child of the layout:
function NotFound() {
return <h1>Page not found</h1>;
}<Route path="*" element={<NotFound />} />Now /nope shows "Page not found". Put the catch-all last in the list, after the real pages. It is the safety net, not a page, and it only runs when nothing else matches.
The complete App.jsx now looks like this, and this exact file was built with npm run build to check it:
import { BrowserRouter, Routes, Route, Link, Outlet, useParams } from "react-router";
const users = [
{ id: "1", name: "Ayesha", role: "Frontend developer" },
{ id: "2", name: "Bilal", role: "Backend developer" },
];
function Layout() {
return (
<div>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link> |{" "}
<Link to="/users">Users</Link>
</nav>
<main>
<Outlet />
</main>
</div>
);
}
function Home() {
return <h1>Welcome to the router demo</h1>;
}
function About() {
return <h1>About this app</h1>;
}
function Users() {
return (
<div>
<h1>Users</h1>
<ul>
{users.map((user) => (
<li key={user.id}>
<Link to={`/users/${user.id}`}>{user.name}</Link>
</li>
))}
</ul>
</div>
);
}
function UserDetail() {
const { id } = useParams();
const user = users.find((u) => u.id === id);
if (!user) {
return <h1>User not found</h1>;
}
return (
<div>
<h1>{user.name}</h1>
<p>Role: {user.role}</p>
<p>User ID: {user.id}</p>
</div>
);
}
function NotFound() {
return <h1>Page not found</h1>;
}
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
<Route path="users" element={<Users />} />
<Route path="users/:id" element={<UserDetail />} />
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
</BrowserRouter>
);
}Why `Link`, and not a plain `<a>` tag?
A Link renders as an <a> tag in the HTML. We checked: the users list renders href="/users/1" on each link. The difference is what happens when you click it. A Link tells the router to swap the page in place, with no reload. A plain <a href="/users/1"> tells the browser to fetch the page again from the server, which reloads the whole app and throws away its state: form inputs, scroll position, anything held in memory.
Use Link for every move inside your app. Save <a> for links that leave your app, like a link to an outside website. If a click ever causes a full page flash, you have an <a> where a Link belongs.
Try it yourself
1. Add a third user to the users array, then open /users/3 and check the page shows the new user. 2. Add a Contact page: a component, a Link in the nav, and a route. Check the address bar shows /contact. 3. Add a "Back to users" Link on the UserDetail page that points to /users.
Common mistakes
- Using
<a href>for links inside your app. The page reloads, the app state resets, and the screen flashes. UseLinkfor every in-app move. - Forgetting
<Outlet />in a layout. The nav shows, the page stays empty, and nothing errors. The child route has nowhere to render. - Writing child paths with a leading slash when you mean them nested. A child path of
/aboutis absolute, so it escapes the layout. Writeaboutand let it join the parent's . - Calling
useParamsin a component that no route renders. It returns an empty object, soidisundefinedand the lookup finds nothing. Only call it inside a routed component. - Forgetting the
keyprop when mapping users to list items. React warns in the console, and list updates can behave oddly. Thekeyis theuser.idin the example above.
Frequently asked questions
Do I need a router for a one-page app?
No. Add it when you have a second page. A single page with sections and anchor links does not need routing, and adding a router early just adds concepts before they pay off.
What is the difference between BrowserRouter and HashRouter?
BrowserRouter uses clean URLs like /users/2, and it needs the server to serve your app for any path when the page refreshes. Vite's dev server does that, and most hosts can be set up to do it. HashRouter puts the route after a #, like /#/users/2, and works on any static host with no server setup. This tutorial uses BrowserRouter, and HashRouter is the easy fallback if your host cannot serve clean URLs.
Can I use React Router with Next.js?
No. Next.js has its own file-based routing, and the two routers do not mix. React Router is for Vite and other plain React setups, which is why the React from Scratch course uses Vite. If you move to Next.js later, you will learn its routing instead.
How do I protect a page so only logged-in users see it?
Wrap the route's component in a check: if the user is not logged in, send them to the login page instead. The login pattern is in Authentication Basics (item 32), and State Management in React (item 55) builds the shared login state that check reads.
Does the back button work?
Yes. Link updates the real browser history, so back and forward work the way users expect. That is the point of routing through the URL instead of hiding and showing parts of one page.
Follow the Full-Stack Developer Roadmap
Build complete web applications, frontend, backend APIs, databases and deployment. Free, step by step, at your pace.
View the Roadmap