What is Routing?
Routing is the system that decides what a user sees for a given URL. On a traditional website, each URL corresponds to a separate HTML file sent by the server. In a single page application built with React, the browser loads one HTML page, and a client-side router swaps components in and out as the URL changes, without asking the server for a new page.
Server routing versus client routing
| Aspect | Server-side routing | Client-side routing |
|---|---|---|
| What happens on a link click | Browser requests a new page and reloads | JavaScript updates the URL and renders new components |
| Who decides the content | The server, per request | The router inside the app |
| Speed after first load | Every page is a full round trip | Instant view changes; only data is fetched |
| State between pages | Lost on every reload | Kept in memory, such as a cart or open menu |
How a client-side router works
The browser's History API lets JavaScript change the address bar with history.pushState without reloading. A router library uses this in three steps. First, it intercepts clicks on its link components and calls the History API instead of letting the browser navigate. Second, it compares the new URL against a list of route definitions. Third, it renders the component that matches. It also listens for the Back and Forward buttons so they move between views as users expect.
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
function Home() { return <h2>Home</h2>; }
function About() { return <h2>About us</h2>; }
function NotFound() { return <h2>Page not found</h2>; }
export default function App() {
return (
<BrowserRouter>
<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>
</BrowserRouter>
);
}
Here BrowserRouter connects the app to the browser's URL, Routes picks the best match, and each Route pairs a path with the element to show. The * path catches anything unmatched.
What a good router gives you
- Bookmarkable views: every screen has its own URL that can be shared or saved.
- Working Back button: users move through history the way they do on any site.
- Dynamic segments: one route can serve many items, like each product's detail page.
- Nested layouts: a dashboard can keep its sidebar while the inner panel changes.
- Code splitting: rarely visited pages can be loaded only when needed.
The full-reload mistake
The most common error when adding routing is using normal anchor tags for internal links: <a href="/about">. The page still reaches the right view, so it seems fine, but every click reloads the entire app, downloads the JavaScript again and throws away all state. Use the router's Link or NavLink components for internal navigation, and keep plain anchors for external sites.
A related surprise appears after deployment: refreshing a URL like /about can give a server 404, because the server has no file at that path. The host must be told to return index.html for unknown paths so the router can take over.
Routing without a library
You could listen to popstate events and switch components manually, and some tiny apps do. Libraries such as React Router handle the many edge cases, including nested routes, parameters, relative links and scroll behaviour, so they are the practical choice for real projects.
Takeaway: routing maps URLs to views; in a React app a client-side router uses the History API to change the URL and render the matching component without reloading, so always navigate with Link rather than plain anchors.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.