Client-side routing concepts
Why a single-page React app needs its own router, the History API it's built on, and what you'd have to hand-build without one.
Client-side routing concepts
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A normal link click or URL change makes the browser throw away the whole page and fetch a new document, which destroys every bit of in-memory React state an SPA worked to keep alive.
- `history.pushState(state, "", url)` changes the address bar and adds a history entry without triggering a browser navigation, which is the one browser primitive every JavaScript router — React Router included — is built on.
- `pushState` fires no event of its own, so a router also has to listen for the `popstate` event to notice when the user moves through history with the Back or Forward button.
- Treating the URL as application state means a given path should always render the same view, which is what makes a route shareable, bookmarkable, and restorable after a refresh.
- Hand-rolling a router means intercepting every internal link click, matching the current path against a list of patterns, extracting dynamic segments, and re-rendering on both `pushState` calls and `popstate` events — work a router library exists to do for you.
- React Router's APIs, covered in the next lesson, are a thin, ergonomic layer over exactly this mechanism, not a different concept.
The problem routing exists to solve
By the time you reach this lesson you’ve built components that hold their own state with useState, synchronize with the outside world through effects, and clean those effects up so a slow request can’t stomp on a fast one. None of that machinery survives a real page navigation. The browser’s default behavior for any URL change — clicking a plain <a href>, typing a new address, submitting a form — is to throw away the current document entirely and request a brand new one from the server. Every component unmounts, every piece of state disappears, and the user sees a blank flash while a fresh HTML page downloads and your JavaScript bundle re-initializes from scratch.
For a traditional, multi-page website that’s fine — it’s literally how the web was designed to work. For a single-page React app it’s a regression. The whole point of building an SPA is that the app loads once and then runs entirely in memory: a half-filled form, a scroll position, an open modal, a cached list of search results — all of that should survive moving from /products to /products/42. If every “page change” is actually a full browser reload, you’ve built a React app that behaves like it isn’t one.
What you’d have to build by hand
Suppose you try to solve this yourself, using only what you already know: state and event handlers. A first attempt might look like this:
function App() {
const [view, setView] = useState("home");
return (
<div>
<button onClick={() => setView("home")}>Home</button>
<button onClick={() => setView("products")}>Products</button>
{view === "home" && <Home />}
{view === "products" && <ProductList />}
</div>
);
}
This genuinely avoids a full page reload — clicking a button just updates state and React re-renders. But look at what’s missing. The URL never changes, so there is nothing to bookmark, nothing to share with a teammate, and nothing for the browser’s Back button to act on; clicking Back would leave the React app running but take the user to whatever page they were on before they ever opened this app. Refreshing the page always lands back on "home", because the current view lives only in a JavaScript variable that disappears on reload. And the moment you need a dynamic piece — /products/42 — you’d have to invent your own syntax for parsing an id out of a string and keep it in sync with view by hand.
The browser API underneath every real router
The fix is to stop treating navigation as “just a state update” and start treating the URL itself as a piece of application state that the browser knows how to persist, bookmark, and navigate through. The browser exposes this through the History API, and the method that matters most is history.pushState:
function navigate(path) {
history.pushState({}, "", path);
render(path); // your own function that decides what to show
}
Calling pushState changes what’s shown in the address bar and adds a new entry to the browser’s session history, but — critically — it does not trigger a reload or fire any event. The page in memory is left completely alone. That’s the whole trick: you get a real, bookmarkable, shareable URL without paying the cost of a server round trip.
But pushState only covers navigation your own code initiates. It says nothing about what happens when the user clicks the browser’s own Back or Forward button. For that there’s a second piece, the popstate event, which fires precisely when the active history entry changes through the browser’s built-in controls rather than through your code:
window.addEventListener("popstate", () => {
render(location.pathname);
});
A working router is really these two pieces wired together, plus one more: intercepting clicks on internal links so they call pushState instead of letting the browser perform its default, reload-triggering navigation.
From this mechanism to a router
Once a URL change is detectable without a reload, “routing” becomes a matching problem: take the current path, compare it against a list of registered patterns (/products/:id, /dashboard/settings), and render whichever component matches — extracting any dynamic segment like :id along the way. Every router you’ll ever use, from React Router to Vue Router to a five-line router you write yourself for a toy project, is doing exactly this. The pattern syntax and the component API differ; the underlying browser contract — pushState to move forward without reloading, popstate to notice when the user moved on their own — does not.
What the video covers
dcode’s walkthrough is a standalone, framework-agnostic tour of the History API: pushState, replaceState, the history stack, and the popstate event, demonstrated with plain JavaScript and no router library in sight. That’s useful precision — it isolates the exact mechanism this lesson describes without any framework’s abstractions blurring it. It doesn’t discuss React or React Router at all, and it doesn’t cover route matching, dynamic segments, or nested layouts; this lesson’s “what you’d build by hand” section and the bridge to router libraries are original material meant to connect the video’s low-level mechanism to the React-specific tools in the next lesson.
Where this fits
With the browser mechanism in hand, the next lesson introduces the library that wraps it for React: React Router fundamentals, where pushState and popstate disappear behind Link, useNavigate, and route matching.
Check yourself
Why can't a single-page app just let the browser handle every navigation normally?
A normal navigation discards the current document and requests a fresh one from the server, which would wipe out all of React's in-memory component state, re-download the JavaScript bundle, and produce a visible blank-page flash on every click — the opposite of what a single-page app is trying to achieve.
What does `history.pushState` actually do, and what does it not do?
It changes the URL shown in the address bar and pushes a new entry onto the browser's session history. It does not navigate anywhere, reload anything, or fire any event — the page in memory is completely untouched until your own code decides to react to the change.
Why does a router need to listen for the `popstate` event in addition to calling `pushState`?
`pushState` only fires when your own code calls it, so it never tells you about the user clicking Back or Forward, which is a built-in browser history navigation rather than a call your code made. `popstate` fires specifically for that case, so a complete router listens for both: `pushState` for programmatic navigation and `popstate` for the user's own history controls.
If you hand-built routing by just swapping components on a button's onClick handler, what would you lose compared to a real router?
The URL in the address bar would never change, so the current view couldn't be bookmarked, shared, or restored on refresh, the Back and Forward buttons would do nothing useful, and you would still need to write matching logic by hand for any dynamic segment like an id — all of which a router is designed to solve once, generically.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.