Explain it simply
Basic routing in plain language
Basic routing is a list that pairs each URL path with a component: / shows Home, /about shows About, and anything else shows 'Page not found'. In React Router you write that list in JSX: <Routes> is the list and each <Route> is one line in it.
To move between screens you use <Link>. It looks like a normal link, but a click changes the URL and shows the matching component without reloading the whole page. It is the job of the tiny router from the previous lesson, done by the library.
Problem → Why → Solution
The problem: every link click restarts the app
The routes are written and the nav bar uses plain <a href> links. The links work, but every click flashes the screen white and wipes the items in the shopping cart and the text typed into a form. The Network tab shows the whole JavaScript bundle being downloaded again on each click.
function Nav() {
return (
<nav>
<a href="/">Home</a> | <a href="/cart">Cart</a> {/* full page loads */}
</nav>
);
}Why <a href> wipes the state
Clicking an <a href> makes the browser request a new page from the server without asking the router. All JavaScript loads again from scratch and React mounts a fresh app, so every piece of in-memory state is gone. <Link> intercepts the click and only changes the URL, so the React app keeps running and the state survives.
Core concept: the router, the route list and links
Install the package
Run npm install react-router in the Vite project folder. In React Router v7 everything a browser app needs is in this one package. Restarting the dev server afterwards is a safe habit.
Wrap the app in BrowserRouter
In main.jsx, wrap <App /> in <BrowserRouter> exactly once. BrowserRouter reads the address bar, listens to the History API and provides routing information to every component below it through context. Rendering <Routes> without a router fails with 'useRoutes() may be used only in the context of a <Router> component'.
Write the route list
Inside <Routes>, add routes such as <Route path="/about" element={<About />} />. On every URL change, Routes looks at all routes and picks the best match, ranked by how specific each path is rather than by the order you wrote them. path="*" catches every URL nothing else matched, which is where a 404 page goes.
Connect screens with Link
<Link to="/about"> renders a real <a href="/about">, so Ctrl-click still opens a new tab, while plain clicks are handled by the router. NavLink is the same as Link, but when it matches the current URL it adds an active class and aria-current="page" automatically.
npm install react-router
npm run devA Route's element prop takes a JSX element (<About />), not the component function, so you can pass props directly, as in <About title="Team" />. The <Switch> and component={About} you may see in v5-era tutorials no longer exist since v6.
BrowserRouter is the right choice for apps served from a normal web server. React Router ships two other routers as well: MemoryRouter keeps the location in memory instead of the address bar, which is handy in tests and component previews, and HashRouter puts the path after a #, for hosts where you cannot configure a fallback rule.
- BrowserRouter reads the URL
- Routes picks the best-matching Route
- That Route's element renders
- Link changes the URL; Routes re-picks
One BrowserRouter, at least one Routes, and Link for every in-app navigation: that is the whole setup.
General pattern
// main.jsx: wrap the whole app once
<BrowserRouter>
<App />
</BrowserRouter>
// App.jsx: one Route per URL pattern, plus a catch-all
<Routes>
<Route path="/some-path" element={<SomePage />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
<Link to="/some-path">Label</Link>Import everything from react-router. Put the router once at the very outside, and Routes wherever the screen should change.
Worked example: Home, About and a 404
The main.jsx below replaces Vite's default entry file. It has three page components, a nav with two Links and three routes. Everything is in one file for clarity; real projects usually give each page its own file under src/pages.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter, Routes, Route, Link } from 'react-router';
function Home() {
return <h1>Home Page</h1>;
}
function About() {
return <h1>About Page</h1>;
}
function NotFound() {
return <h1>404: Page not found</h1>;
}
function App() {
return (
<>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Routes>
</>
);
}
createRoot(document.getElementById('root')).render(
<StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>
);Code breakdown
- One import source
import { BrowserRouter, Routes, Route, Link } from 'react-router';In v7 everything, BrowserRouter included, comes from react-router.
- Links instead of anchors
<Link to="/">Home</Link> | <Link to="/about">About</Link>The nav sits outside Routes, so it stays on every screen. Clicking a Link changes only the URL; the page does not reload.
- Path to element
<Route path="/about" element={<About />} />When the URL is /about, Routes picks this route and renders <About />.
- Catch-all 404
<Route path="*" element={<NotFound />} />A URL that matches no other route, such as /pricing, lands here. Without it, the nav would show above an empty content area.
- Router at the root
<BrowserRouter>Wrapping App in BrowserRouter inside createRoot(...).render() gives every Link and Routes below it access to the router context.
A nav with Home and About links appears. / shows 'Home Page' and /about shows 'About Page'; clicking a link changes only the heading, with no page reload. Any other URL, such as /pricing, shows '404: Page not found'.Layout routes, index routes and NavLink
To show the same header and nav on every page, you do not repeat them in each page component. Nest child routes inside a parent <Route element={<Layout />}> that has no path, put <Outlet /> inside Layout, and the matching child page appears where the Outlet is. An index route is the default child for the parent's own path (here /), and child paths written without a leading / are joined onto the parent path.
import { Routes, Route, NavLink, Outlet } from 'react-router';
function Layout() {
return (
<>
<nav>
<NavLink to="/" end>Home</NavLink> | <NavLink to="/about">About</NavLink>
</nav>
<main>
<Outlet />
</main>
</>
);
}
export default function App() {
return (
<Routes>
<Route element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
);
}NavLink gives the active link class="active" and aria-current="page", so you only need to style .active in CSS. Notice end on the / link: without it every URL starts with /, so Home would look active on every page.
In real projects
Route setup in real projects
In a real project the routing setup becomes the map of the app. A developer new to the team can read the route list and immediately see which screens exist, so it is worth keeping tidy.
- Folder layout: one file per page, such as src/pages/Home.jsx and src/pages/About.jsx, with the route list kept in one place in App.jsx.
- Shared layouts: keep the header, sidebar and footer in layout routes; teams often have two, one for marketing pages and one for dashboard pages.
- 404 page: render a friendly page with a 'Back to home' Link in the path="*" route, so mistyped URLs and deleted pages do not strand users.
- Navigation style: use NavLink in the nav and make .active bold or underlined; thanks to aria-current, screen readers also announce the current page.
Common mistakes
Common mistakes when setting up routes
Using <a href> for in-app links
As the problem section showed, <a href> causes a full page reload and wipes all state. Use Link or NavLink for navigation inside the app, and keep <a href> for external links to other websites.
Forgetting BrowserRouter, or adding two
Rendering Routes or Link without BrowserRouter fails because there is no router context. The opposite slip, adding a second router inside some component, fails with an error saying you cannot render a <Router> inside another <Router>. Put it in main.jsx exactly once.
Forgetting <Outlet /> in a layout
If you nested routes and the layout's nav appears but no page content shows on any URL, the layout component is usually missing <Outlet />. The Outlet is the placeholder that says where the child route's element goes.
Mini challenge
Mini challenge: nested dashboard routes
Build a site layout with a dashboard section inside it that has its own sub-nav. / shows Welcome, /dashboard shows Overview, /dashboard/settings shows Settings, and unknown URLs show a 404.
- A layout route without a path shows a top nav of NavLinks and an <Outlet />
- The dashboard route shows a DashboardLayout with an index child (Overview) and a settings child
- Add end to the Home and Overview NavLinks so they are not active everywhere
- A path="*" route shows '404: Page not found'
import { BrowserRouter, Routes, Route, NavLink, Outlet } from 'react-router';
function SiteLayout() {
// TODO: NavLinks to "/" (with end) and "/dashboard", then an Outlet inside <main>
return null;
}
function DashboardLayout() {
// TODO: <h1>Dashboard</h1>, NavLinks to Overview and Settings, then an Outlet
return null;
}
const Home = () => <h1>Welcome</h1>;
const Overview = () => <p>3 open tasks</p>;
const Settings = () => <p>Notification settings</p>;
const NotFound = () => <h1>404: Page not found</h1>;
export default function App() {
// TODO: BrowserRouter > Routes > layout route > index, dashboard (+ children), "*"
return null;
}Expected: / shows the top nav and 'Welcome' with Home active. /dashboard shows the 'Dashboard' heading, the sub-nav and '3 open tasks'. /dashboard/settings shows 'Notification settings' with the Settings link active. /zzz shows '404: Page not found' under the nav.
Show hint
Write child paths without a leading / ("dashboard", "settings"); they are joined onto the parent path automatically. Whenever a Route contains Routes, the parent element needs an <Outlet />.
Show solution
import { BrowserRouter, Routes, Route, NavLink, Outlet } from 'react-router';
function SiteLayout() {
return (
<>
<nav>
<NavLink to="/" end>Home</NavLink> | <NavLink to="/dashboard">Dashboard</NavLink>
</nav>
<main>
<Outlet />
</main>
</>
);
}
function DashboardLayout() {
return (
<section>
<h1>Dashboard</h1>
<NavLink to="/dashboard" end>Overview</NavLink> |{' '}
<NavLink to="/dashboard/settings">Settings</NavLink>
<Outlet />
</section>
);
}
const Home = () => <h1>Welcome</h1>;
const Overview = () => <p>3 open tasks</p>;
const Settings = () => <p>Notification settings</p>;
const NotFound = () => <h1>404: Page not found</h1>;
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route element={<SiteLayout />}>
<Route index element={<Home />} />
<Route path="dashboard" element={<DashboardLayout />}>
<Route index element={<Overview />} />
<Route path="settings" element={<Settings />} />
</Route>
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
</BrowserRouter>
);
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Install with npm install react-router and add <BrowserRouter> once in main.jsx
- <Routes> picks the <Route> that best matches the URL and renders its element
- Use <Link> or <NavLink> for in-app navigation; <a href> reloads the page
- path="*" is for the 404 page, and layout routes render the child page where <Outlet /> is
React Router Documentation — React Router