React Mastery / Routing (React Router)

Introduction to React Router

🌐 What Is Routing?

Routing is the mechanism that decides what content to show based on the URL.

Example:

  • / β†’ Home Page

  • /about β†’ About Page

  • /profile β†’ Profile Page

In traditional websites, each URL loads a new HTML page.

In React, routing usually happens without reloading the page.


πŸ†š SPA vs MPA

🧩 MPA (Multi Page Application)

  • Each page is a separate HTML file

  • Full page reload on every navigation

  • Slower user experience

  • Server handles routing

Example: Traditional PHP, JSP, or older websites

⚑ SPA (Single Page Application)

  • Only one HTML file

  • Page content changes dynamically

  • No full page reload

  • Faster and smoother UX

  • Routing handled on the client side

React applications are SPAs!!


❓ Why Routing Is Needed in React

Without routing:

  • React apps would be stuck on one screen

  • You’d have to manually manage views using conditions

With routing:

  • Clean URLs (/login, /dashboard)

  • Better navigation

  • Browser back/forward buttons work

  • Easy to scale large applications


🧠 Client-Side Routing Concept

In React:

  • The browser URL changes

  • React decides which component to render

  • No request is sent to the server for a new page

πŸ‘‰ JavaScript intercepts navigation and updates the UI.


πŸ”‘ Key Concepts

πŸ”— URL

  • Represents the current route

  • Example: https://example.com/products/1


πŸ•’ Browser History

  • Maintains navigation stack

  • Enables:

    • Back button

    • Forward button

    • Programmatic navigation


πŸ”„ Page Refresh vs Route Change

Action

What Happens

Page Refresh

App reloads, state resets

Route Change

Component updates, state preserved


πŸ“¦ What Is react-router-dom?

react-router-dom is the official routing library for React web apps.

It provides:

  • Routing components

  • Navigation helpers

  • Hooks for routing logic


πŸ” Router Types Overview

🌍 BrowserRouter

  • Uses HTML5 History API

  • Clean URLs (/about)

  • Most commonly used

  • Recommended for modern apps

πŸ”— HashRouter

  • Uses hash (#/about)

  • Works without server configuration

  • Used in static hosting environments


πŸ“ Recommended Folder Structure

src/
 β”œβ”€ pages/
 β”‚   β”œβ”€ Home.jsx
 β”‚   β”œβ”€ About.jsx
 β”‚   └─ Contact.jsx
 β”œβ”€ App.jsx
 └─ main.jsx

πŸ‘‰ Each page = one route


πŸ› οΈ Hands-On: Setup React Router

1️⃣ Install React Router

npm install react-router-dom@6

2️⃣ Wrap App with BrowserRouter

main.jsx

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')).render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

πŸ‘‰ Code Explanation

BrowserRouter is the traffic controller that tells React which page to show for which URL.

What it’s doing here 🧠

<BrowserRouter>
	<App />
</BrowserRouter>
  • It listens to the browser URL (/, /about, /profile)

  • It keeps UI in sync with the URL

  • It lets React Router features work:

    • <Routes> / <Route>

    • <Link> / <NavLink>

    • useNavigate, useParams

Without it ❌

➑️ React has no idea the URL changed.


πŸ‘€ We’ll focus on React Router v6 because:

  • It’s stable

  • Widely used in production

  • Easier for learning fundamentals