React Course
React
/
Intermediate

React Router

Definition

React Router is the standard routing library for React. It keeps the UI in sync with the URL, allowing you to build Single Page Applications (SPAs) with navigation without refreshing the page.

Explain Like I'm New

In a traditional website, clicking a link forces the browser to download a completely new HTML page from the server, causing the screen to flash white. In a React Single Page Application (SPA), there is only ONE HTML page. React Router simply watches the URL bar. If you click a link to `/about`, React Router instantly hides the 'Home' component and shows the 'About' component, making navigation feel lightning fast without a page reload.

Real World Example

When you use Twitter or Gmail, clicking on a profile or an email changes the URL (e.g., `twitter.com/user/123`), but the music you are listening to doesn't stop and the sidebar doesn't flash. React Router is handling the URL change and swapping out the middle content dynamically.

Common Use Cases

  • Building Single Page Applications (SPAs) with multiple views
  • Extracting dynamic parameters from the URL (like Product IDs)
  • Protecting routes (e.g., redirecting unauthenticated users from `/dashboard` to `/login`)

Interactive Example

import { BrowserRouter, Routes, Route, Link, useParams, useNavigate } from 'react-router-dom';

// 1. A Component using URL Parameters
function UserProfile() {
  const { userId } = useParams(); // Extracts the dynamic part of the URL
  return <h2>Viewing Profile for User: {userId}</h2>;
}

// 2. A Component with Programmatic Navigation
function Login() {
  const navigate = useNavigate();
  const handleLogin = () => {
    // Simulate API call
    setTimeout(() => navigate('/dashboard'), 1000); 
  };
  return <button onClick={handleLogin}>Log In</button>;
}

// 3. The Router Setup
export default function App() {
  return (
    <BrowserRouter>
      <nav>
        {/* Use <Link> instead of <a> to avoid full page reloads */}
        <Link to="/">Home</Link> | 
        <Link to="/user/123">Profile 123</Link> | 
        <Link to="/login">Login</Link>
      </nav>

      <Routes>
        <Route path="/" element={<h1>Home Page</h1>} />
        <Route path="/login" element={<Login />} />
        {/* The :userId is a dynamic URL parameter */}
        <Route path="/user/:userId" element={<UserProfile />} />
        {/* A Catch-All route for 404s */}
        <Route path="*" element={<h2>404 Not Found</h2>} />
      </Routes>
    </BrowserRouter>
  );
}

Interview Questions

basic

  • What is the difference between an `<a>` tag and React Router's `<Link>` component?
  • How do you define a basic route in React Router v6?

intermediate

  • What are URL Parameters and how do you access them?
  • How do you navigate programmatically (e.g., redirecting after a successful login)?

advanced

  • What is Nested Routing and what is the `<Outlet>` component used for?
  • How does Client-Side Routing actually work under the hood in the browser?

trick

  • If you refresh a page on a React Router SPA hosted on a static server (like S3), why might you get a 404 error?

Flash Cards

Question

What is the difference between <a> and <Link>?

Click to reveal answer
Answer

An `<a>` tag triggers a full page refresh by making a new request to the server. A `<Link>` component intercepts the click, updates the URL using the browser's History API, and tells React to re-render the correct component immediately without a refresh.

Question

How do you navigate programmatically?

Click to reveal answer
Answer

In React Router v6, you use the `useNavigate` hook. `const navigate = useNavigate();` and then call `navigate('/dashboard')` when a form submits successfully.

Question

Why might refreshing a page give a 404 error on a static server?

Click to reveal answer
Answer

Because the server is looking for a physical file at that path (e.g., `/dashboard.html`). In an SPA, only `index.html` exists. You must configure the server to always redirect 404 requests back to `index.html` so React Router can take over.