React Router
Client-side routing with React Router, route definitions, navigation, and URL parameters.
What you'll learn
- Setting up React Router with Routes and Route
- Navigating with Link and useNavigate
- Reading URL parameters with useParams
- Nested routes and layout routes
Concept
What Is React Router?
React Router is the standard library for client-side routing in React applications. It lets you map URL paths to components, so different URLs render different pages without a full page reload. This creates a single-page application (SPA) experience where navigation feels instant. React Router v6 (and the newer v7) introduced a data-loading API and simplified the route definition syntax.
Defining Routes
You wrap your application in a <BrowserRouter> and define routes with <Routes> and <Route>. Each <Route> has a path and an element prop. React Router matches the current URL against the route paths and renders the matching element. Routes can be nested — a parent route renders an <Outlet /> where child routes appear. This lets you share layouts (headers, sidebars) across multiple pages.
Navigation
Use the <Link> component for navigation — it renders an <a> tag but intercepts the click to do client-side routing without a full reload. For programmatic navigation (after a form submit, for example), use the useNavigate() hook to get a navigate function. You can pass state through navigation: navigate("/dashboard", { state: { fromLogin: true } }) and read it with useLocation().state.
URL Parameters
Dynamic route segments are defined with a colon: <Route path="/users/:id" element={<UserProfile />} />. Inside the component, useParams() returns { id: "123" }. Query parameters are accessed via useSearchParams(), which returns a URLSearchParams instance you can read and update. URL parameters are the primary way to make your app's state shareable and bookmarkable.
Navigation Guards and Loaders
React Router v6.4+ introduced data APIs: loader functions fetch data before a route renders, and action functions handle form submissions. These replace the need for useEffect-based fetching in route components. Navigation guards can be implemented with the useBlocker hook (in newer versions) or by checking conditions in loaders. These APIs bring React Router closer to framework-level routing like Next.js.
Example
import { BrowserRouter, Routes, Route, Link, useParams, Outlet } from "react-router-dom";
function Layout() {
return (
<div>
<nav>
<Link to="/">Home</Link>
<Link to="/users">Users</Link>
</nav>
<Outlet /> {/* Child routes render here */}
</div>
);
}
function UserProfile() {
const { id } = useParams(); // Read the :id URL parameter.
return <h1>User Profile for ID: {id}</h1>;
}
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<h1>Home Page</h1>} />
<Route path="users" element={<h1>All Users</h1>} />
<Route path="users/:id" element={<UserProfile />} />
<Route path="*" element={<h1>404 Not Found</h1>} />
</Route>
</Routes>
</BrowserRouter>
);
}
BrowserRouter wraps the app. Layout renders shared nav with an Outlet for child routes. UserProfile reads the :id param. The * route catches unmatched URLs.
Try it
- Regex Tester
Test regex patterns for validating route paths and URL parameters.
- JSON Formatter
Format route configuration objects when building dynamic routes.
Common mistakes
The mistake
Using <a> tags instead of <Link> for internal navigation
The fix
Regular <a> tags cause a full page reload, losing client state. Use <Link to="/path"> for internal navigation. Use <a> only for external links.
The mistake
Forgetting to wrap the app in BrowserRouter
The fix
Router hooks like useNavigate and useParams only work inside a Router context. Wrap your app (or the routing portion) in <BrowserRouter> at the top level.
Practice React Router
6 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.