🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

Routing

AI & DATA SCIENCE // routing

Client-side routing lets a single-page React application show different views for different URLs without a full page reload, typically handled by the React Router library.

Syntax

npm install react-router-dom

Deep Dive Course

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.

editor.html
# Terminal
npm install react-router-dom
localhost:3000

2Practical Example

Here is a real-world application of Routing showing how it is used in production React code.

editor.html
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}
localhost:3000

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.

editor.html
# Terminal
npm install react-router-dom
localhost:3000

Examples

Example 01Basic Usage
# Terminal
npm install react-router-dom
Example 02Advanced Example
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}