Why Routing?
Routing is what lets a single-page React app show different screens for different URLs. Without it, your whole app lives at one address, so users cannot bookmark a page, share a link to a product, use the back button sensibly or open something in a new tab. Client-side routing gives you those familiar browser behaviours while keeping the fast, no-reload feel of a single-page app.
The problem with one URL for everything
A simple way to switch screens is to store the current page in state and render a component based on it: {page === 'cart' ? <Cart /> : <Home />}. It works for a demo, but the address bar never changes. That causes real problems:
- Refreshing the page always drops the user back on the home screen.
- The back button leaves the app entirely instead of returning to the previous screen.
- Links cannot point to a particular screen, so sharing and bookmarking are impossible.
- Search engines and analytics see only one page.
What a router gives you
A routing library such as React Router keeps the URL and the screen in sync. When the user clicks a link, it updates the address with the browser's History API and renders the matching component. When the user presses back or forward, it listens for the change and renders the right screen again, all without asking the server for a new HTML page.
| Feature | Benefit to users |
|---|---|
| URLs per screen | Bookmarks, shareable links, new tabs |
| History integration | Back and forward buttons behave as expected |
| URL parameters | One component can show any product, like /products/:id |
| Nested layouts | Headers and sidebars stay while inner content changes |
| Protected routes | Redirect signed-out users away from private pages |
| Code splitting by route | Load each screen's code only when it is visited |
Client-side versus server-side routing
On a traditional website, every link sends a request to the server, which returns a whole new HTML page. The browser clears the old page, loses any JavaScript state and draws everything again. In client-side routing, the server sends one HTML shell and the JavaScript bundle once; after that, the router swaps components in the browser. Transitions feel instant, and state such as a half-filled form in a sidebar or an open audio player survives navigation.
// Without routing: state decides the screen, URL never changes
function App() {
const [page, setPage] = useState('home');
return (
<>
<button onClick={() => setPage('home')}>Home</button>
<button onClick={() => setPage('cart')}>Cart</button>
{page === 'home' ? <Home /> : <Cart />}
</>
);
}
// With React Router: the URL decides the screen
// / -> <Home />
// /cart -> <Cart />
// <Link to="/cart">Cart</Link> updates the address bar and the view
Trade-offs to keep in mind
Client-side routing has costs. The first load downloads more JavaScript before anything appears, which is why route-based code splitting helps. Content rendered only in the browser can be harder for some crawlers to read, which is one reason frameworks built on React add server rendering.
There is also a classic deployment trap. Since the server only knows about index.html, a refresh on /cart asks the server for a file that does not exist and returns a 404. The server needs a fallback rule that serves index.html for every app route, so the router can take over.
Finally, routing is not a security layer. Hiding a route in the browser does not protect data; the API must still check whether the user is allowed to see it.
The short answer
You need routing when your app has more than one screen that users might want to reach directly. It ties screens to URLs so links, refreshes and the back button all behave the way people expect from the web.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.