Sub topic 1
How client-side routing works
Prerequisites
This chapter assumes you know JavaScript (ES modules, async/await, fetch), React function components and JSX, basic hooks such as useState and useEffect, and how URLs and the browser's back button behave.
Server-rendered navigation
In a traditional site, every link click sends a new request to the server. The server returns a full HTML document, and the browser throws away the current page and builds a new one. Scripts reload, and any in-memory state is lost.
Client-side navigation
In a single-page app (SPA), the server sends one HTML page and a JavaScript bundle. After that, your code handles navigation. When the user clicks a link, the router does three things:
- Stops the browser's default full-page request.
- Changes the address bar URL without reloading.
- Renders the React components that match the new URL.
Only the parts of the screen that change are updated. State held higher in the tree survives, and no new HTML document is fetched.
The History API
Routers rely on the browser's History API. Two methods matter most:
history.pushState(state, "", url)adds an entry to the history stack and changes the URL, with no network request.history.replaceState(state, "", url)changes the current entry instead of adding one.
When the user presses back or forward, the browser fires a popstate event. The router listens for it, reads location.pathname, and renders the matching routes.
// What a router does at its core, without React Router
function navigate(url) {
window.history.pushState({}, "", url);
render(window.location.pathname);
}
window.addEventListener("popstate", () => {
render(window.location.pathname);
});
function render(pathname) {
document.body.textContent = `You are at ${pathname}`;
}
render(window.location.pathname);React Router wraps this pattern. It adds route matching, nested layouts, data loading, and form handling, so you do not write it yourself.
Common errors
- 404 on refresh. Visiting
/aboutdirectly makes the browser ask the server for/about. The server has no such file, because only your app knows that route. Configure the host to serveindex.htmlfor unknown paths. - Using plain
<a>tags. An<a href>triggers a full page load and discards app state. React Router'sLinkcomponent avoids this, and a later chapter covers it. - Expecting a reload. Since the page does not reload, code that runs once on load will not run again on navigation. Use route-level data loading or effects tied to the URL.
What comes next
The next chapters set up a router with createBrowserRouter and RouterProvider, then define routes that map URLs to components.
Exercise
A user opens https://example.com/about in a new tab and gets a server 404, even though clicking an About link inside the app works. Why does this happen, and how do you fix it?
Show answer
Clicking the link uses history.pushState, so the server is never asked for /about and the router renders the About page. Opening the URL in a new tab sends a real request to the server, which has no /about file. Fix it by configuring the server or host to return index.html for unknown paths, so the app loads and the router handles the URL.
Exercise
Which browser event does a router listen for to react to the back and forward buttons?
Show answer
The popstate event. It fires when the active history entry changes through back or forward, and the router then reads window.location and renders the matching routes.