Because a React single-page application is really just one HTML page that JavaScript dynamically updates, it needs its own mechanism to keep the browser's URL bar in sync with what's currently displayed, and to update the displayed content when the URL changes, whether from a link click or the browser's back/forward buttons — that's exactly what client-side routing provides. React Router, the de facto standard routing library for React, uses the browser's History API under the hood to update the URL without triggering an actual server round-trip and full page reload, intercepting navigation and rendering the appropriate component tree for the current URL instead.
1Understanding Routing
Because a React single-page application is really just one HTML page that JavaScript dynamically updates, it needs its own mechanism to keep the browser's URL bar in sync with what's currently displayed, and to update the displayed content when the URL changes, whether from a link click or the browser's back/forward buttons — that's exactly what client-side routing provides. React Router, the de facto standard routing library for React, uses the browser's History API under the hood to update the URL without triggering an actual server round-trip and full page reload, intercepting navigation and rendering the appropriate component tree for the current URL instead.
Client-side routing exists specifically to avoid full page reloads on navigation — if a navigation still causes a full page refresh, something is bypassing React Router's routing mechanism, like a plain <a> tag instead of React Router's Link component.
# Terminal
npm install react-router-dom2Practical Example
Here is a real-world application of Routing showing how it is used in production React code.
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}3Best Practices
Follow these guidelines when working with Routing:
1. Use React Router's Link component instead of a plain <a> tag for internal navigation, so routing stays client-side without a full page reload
2. Wrap the application in a BrowserRouter near the root, so routing components anywhere in the tree can access the current location
3. Define routes declaratively with Route elements, matching URL patterns to the components that should render for them
Tip: Client-side routing exists specifically to avoid full page reloads on navigation — if a navigation still causes a full page refresh, something is bypassing React Router's routing mechanism, like a plain <a> tag instead of React Router's Link component.
# Terminal
npm install react-router-dom