Lesson 31/100

Tutorials React.js Tutorial

React Router — Complete Guide

React Router — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 31 of 100

React Router

Beginner ✓IntermediateAdvancedProfessional

Intermediate · 2 — Build apps · ~14 min read · Module 4: Routing & Data

Introduction

You know the basics now. Here we use React Router in real app situations — forms, pages, and data. Still plain language, just a bit more depth. React Router maps URLs to components. When the path is /about, it shows the About page component without a full browser reload. Multi-page feel in a single-page app — users can bookmark /dashboard and use the back button.

Routing is how a single React app feels like many pages. Users expect URLs like /cart and /checkout — Router makes that work.

When will you use this?

Add routing when your app has more than one screen and URLs should be bookmarkable.

  • Multi-page apps like dashboards use React Router — /settings, /profile, /orders.
  • E-commerce sites route /product/123 to a product detail component.

Real-world: Toolliyo course SPA routes

Toolliyo is one React app with URLs /courses, /courses/:id, /profile, /quiz. React Router maps each path to a page component — bookmarks and back button work.

Production-style code

import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <SiteHeader />
      <Routes>
        <Route path="/" element={<HomePage />} />
        <Route path="/courses" element={<CourseCatalog />} />
        <Route path="/courses/:courseId" element={<CoursePlayer />} />
        <Route path="/profile" element={<ProfilePage />} />
        <Route path="*" element={<Navigate to="/" replace />} />
      </Routes>
    </BrowserRouter>
  );
}

What happens in production: Students share lesson URLs; marketing runs ads to /courses/react — routing is how SPAs feel like multi-page websites.

Lesson example (start here)

Copy this smaller example first. Once it works, compare it with the real-world code above.

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

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

Line-by-line walkthrough

CodeWhat it means
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';Imports code from React or another file so you can use it here.
function App() {Defines a function — often a component or event handler.
return (Sends UI or a value back to whoever called this function.
<BrowserRouter>React Router — maps URLs to page components.
<nav>JSX tag — a UI element or custom component on the page.
<Link to="/">Home</Link>React Router — maps URLs to page components.
<Link to="/about">About</Link>React Router — maps URLs to page components.
</nav>Part of the React Router example — read it together with the lines before and after.
<Routes>React Router — maps URLs to page components.
<Route path="/" element={<Home />} />React Router — maps URLs to page components.
<Route path="/about" element={<About />} />React Router — maps URLs to page components.
</Routes>React Router — maps URLs to page components.
</BrowserRouter>React Router — maps URLs to page components.
);Closes a block started by { or ( above.

How it works (big picture)

  • BrowserRouter wraps the app.
  • Routes picks the first matching Route.
  • Link navigates without refresh.

Do this on your computer

  1. npm install react-router-dom
  2. Wrap app in BrowserRouter.
  3. Add Routes with path and element.
  4. Use Link instead of for internal links.
  5. Read the real-world section and name which part of the app uses this topic.
  6. Run the example locally and confirm the same behavior in the browser.
  7. Change one value in the example (text, initial state, or URL) and predict what will happen before you save.

Experiments — try changing this

  • Change text or labels in the example and save — watch the browser update.
  • Break the code on purpose (remove a bracket), read the error message, then fix it.
  • Open React DevTools (browser extension) while running React Router and inspect component props/state.

Remember

React Router = URL → component. Use Link for in-app navigation. Routes + Route define the map.

Common questions

BrowserRouter vs HashRouter?

BrowserRouter uses clean URLs; HashRouter uses #/ paths for static hosting without server config.

How long should I spend on React Router?

Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.

What if I get stuck on React Router?

Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.

Where is React Router used in real jobs?

See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details