What is navigation?
Navigation is moving between views of a single-page app by changing the URL without a full page reload. Routers intercept link clicks and programmatic calls, update the browser history, and render the matching route. In React Router you navigate with <Link>, <NavLink>, <Form>, useNavigate(), <Navigate> and redirect().
Ways to navigate
- Declarative links:
<Link to="/about">renders an<a>but handles clicks client-side.<NavLink>adds active and pending styling. - Programmatic:
const navigate = useNavigate(); navigate("/checkout"),navigate(-1)to go back,{ replace: true }to replace the history entry. - Redirects during data loading:
throw redirect("/login")in loaders, actions or middleware. - Forms:
<Form method="post">submits to a route action and then revalidates data. - Render-time redirect:
<Navigate to="/home" replace />.
Pending UI
useNavigation() exposes state ("idle" | "loading" | "submitting") to show global progress bars, and NavLink receives isPending. Navigations are wrapped in React transitions, so the current page stays visible while the next one loads.
Scroll and focus
<ScrollRestoration /> restores scroll positions; for accessibility, move focus to the new page's heading after navigation.
Native APIs
Under the hood routers use the History API (pushState, popstate). Modern browsers also have the Navigation API, which some routers are starting to use.
Prefer links
Use <Link> for anything that is a link, so users can open it in a new tab and screen readers announce it correctly; reserve navigate() for navigation that follows logic (after a successful save).
Code example
1import { Form, Link, NavLink, useNavigate, useNavigation } from "react-router";23export function Header() {4 const navigation = useNavigation();5 return (6 <header>7 {navigation.state !== "idle" && <div className="progress-bar" aria-hidden />}8 <NavLink to="/" end className={({ isActive }) => (isActive ? "font-bold" : "")}>Home</NavLink>9 <NavLink to="/orders" className={({ isPending }) => (isPending ? "opacity-50" : "")}>Orders</NavLink>10 <Link to="/help" target="_blank">Help</Link>11 </header>12 );13}1415export function SaveDraft() {16 const navigate = useNavigate();17 async function save() {18 await fetch("/api/drafts", { method: "POST" });19 navigate("/drafts", { replace: true }); // programmatic, after logic20 }21 return (22 <>23 <button onClick={save}>Save</button>24 <button onClick={() => navigate(-1)}>Back</button>25 <Form method="post" action="/logout"><button>Log out</button></Form>26 </>27 );28}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.