React Router fundamentals

Intermediate · 49 min

Declaring routes with createBrowserRouter, navigating with Link and useNavigate, reading params with useParams — and the framework-mode alternative.

Before thisClient-side routing conceptsComponents and props

PedroTech · 51 min · from 2:19

React Router fundamentals

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PedroTechReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `createBrowserRouter` takes an array of route objects, each pairing a `path` with an `element`, and `<RouterProvider>` renders whatever matches the current URL — this is React Router's classic declarative API.
  • The equivalent, older JSX form nests `<Route path="..." element={...} />` elements inside `<Routes>` inside `<BrowserRouter>`; both forms describe the same route table, just as data versus as markup.
  • `<Link to="/products">` renders an `<a>` tag but intercepts the click with `pushState` instead of letting the browser reload — this is the router applying the History API mechanism from the previous lesson.
  • `<NavLink>` is `<Link>` plus automatic 'active' styling, so a nav item can highlight itself when its route matches the current URL without you tracking that by hand.
  • `useParams()` reads the dynamic segments of the matched route (the `:id` in `/products/:id`) as a plain object, and `useNavigate()` lets you trigger a navigation imperatively from an event handler instead of rendering a `<Link>`.
  • React Router also ships a 'framework mode' — file- and config-based routing where routes are mostly inferred from a `routes.ts` file — but the declarative array-of-objects or JSX form above is still the core API underneath it and the one worth learning first.

From the History API to a router library

The previous lesson ended with pushState and popstate wired together by hand: a function that changes the URL, and a listener that re-renders when it changes. React Router is that same idea, packaged as a library that does the matching, the click interception, and the re-rendering for you, with an API designed around components and hooks instead of raw event listeners.

React Router ships (and has shipped across several major versions) more than one way to declare your routes. This lesson teaches the declarative form — routes written directly as data or JSX in your own code — because it’s the form that maps most directly onto what you already understand about components and props, and because Link, useParams, and useNavigate work identically no matter which form declares the routes underneath them.

Declaring routes with createBrowserRouter

The modern declarative API describes your routes as a plain array of objects, then hands that array to createBrowserRouter:

import { createBrowserRouter, RouterProvider } from "react-router-dom";

const router = createBrowserRouter([
  { path: "/", element: <Home /> },
  { path: "/products", element: <ProductList /> },
  { path: "/products/:id", element: <ProductDetail /> },
]);

function App() {
  return <RouterProvider router={router} />;
}

Each object pairs a path pattern with the element to render when that path matches the current URL. :id is a dynamic segment — it matches any value in that position (/products/42, /products/lamp-01) and makes it available to the matched component. <RouterProvider> is the piece that actually watches the URL, runs the matching, and renders the winning element; without it, the route array is just inert data.

The equivalent classic JSX form

Before createBrowserRouter existed, and still fully supported today, the same route table can be written as nested JSX instead of an array:

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

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/products" element={<ProductList />} />
        <Route path="/products/:id" element={<ProductDetail />} />
      </Routes>
    </BrowserRouter>
  );
}

This is exactly the same declaration, just shaped as markup instead of an array literal: <BrowserRouter> supplies the History-API wiring, <Routes> picks the one <Route> whose path matches, and each <Route>’s element is what gets rendered. If you’ve seen React Router tutorials that build their route table through a routes.ts config file generating a file-based tree, that’s “framework mode” — a newer, separate way of producing a route table, covered briefly below. Both of the forms above are the declarative API this lesson is scoped to, and they’re interchangeable: pick whichever reads more naturally for your project, since the router behaves identically either way.

Link is the router’s replacement for a plain anchor tag:

import { Link, NavLink } from "react-router-dom";

function Nav() {
  return (
    <nav>
      <Link to="/">Home</Link>
      <NavLink
        to="/products"
        className={({ isActive }) => (isActive ? "active" : "")}
      >
        Products
      </NavLink>
    </nav>
  );
}

Link renders a real <a href> — so middle-click, right-click “open in new tab,” and screen readers all still work — but intercepts a plain left click and performs a pushState-based navigation instead of letting the browser reload. NavLink is Link plus one convenience: it calls your className (or style) function with an isActive flag so you can highlight the current section of a nav bar without tracking the current path yourself.

Sometimes navigation needs to happen from code rather than a click — after a form submits successfully, for example — and that’s what useNavigate is for:

import { useNavigate } from "react-router-dom";

function LoginForm() {
  const navigate = useNavigate();

  function handleSubmit(e) {
    e.preventDefault();
    // ...authenticate...
    navigate("/dashboard");
  }

  return <form onSubmit={handleSubmit}>{/* ... */}</form>;
}

Reading the dynamic part of the URL

Back in ProductDetail, the :id segment declared in the route is read with useParams:

import { useParams } from "react-router-dom";

function ProductDetail() {
  const { id } = useParams();
  return <p>Showing product {id}</p>;
}

useParams() returns a plain object keyed by the dynamic segment names in whichever route matched, as strings — so for /products/42 matched against /products/:id, useParams() returns { id: "42" }.

What the video covers

PedroTech’s React Router v7 tutorial builds a real app’s routing from scratch, and the segment this lesson draws on (roughly its opening chapters through the route-params section) covers project structure, creating routes, and useParams at a practical, beginner-accessible pace from a channel with a strong track record on current React tooling. There is one important gap this lesson fills in rather than leaves to the video: PedroTech builds the app in React Router’s framework mode — routes generated from a routes.ts config rather than written directly as createBrowserRouter calls or JSX — because that’s what current, well-produced React Router v7 content almost universally demonstrates. Link, NavLink, useParams, and useNavigate behave identically either way, so that part of the video transfers directly. But framework mode is not the same thing as the classic declarative API this lesson is scoped to teach, so the createBrowserRouter and <Routes>/<Route> code above is original to this lesson, written to show you the API directly rather than through the video’s file-convention lens. Treat framework mode as a second, more opinionated way of producing the same route table — worth knowing exists, not worth learning in depth until you’re comfortable with the API underneath it.

Where this fits

A flat list of routes handles independent pages, but most real apps share structure — a sidebar that stays put while the content area changes. That’s the subject of the next lesson: nested routes and layouts.

Check yourself

  1. 01In the declarative `createBrowserRouter` API, what two things does each entry in the route array need at minimum?

    A `path` string to match against the current URL and an `element` (a React element to render) or `Component` to mount when that path matches.

  2. 02Why does clicking a `<Link>` not cause the browser's usual full-page reload the way a plain `<a href>` would?

    `<Link>` intercepts the click event, calls `event.preventDefault()`, and then calls the router's `pushState`-based navigation internally instead of letting the browser perform its default anchor-tag navigation, which is the only reason the URL can change without a reload.

  3. 03If a route is declared as `/products/:id`, how do you read the `id` for the currently rendered page inside that route's component?

    Call `useParams()` inside the component; it returns an object whose keys match the dynamic segment names in the route path, so `useParams().id` gives you the matched value as a string.

  4. 04What is 'framework mode' in React Router, and should you learn it before the declarative API shown in this lesson?

    Framework mode is React Router's newer, file- and config-based way of defining routes, where a `routes.ts` file (or a folder convention) generates the route table instead of you writing it as JSX or a plain array. It's an alternative on top of the same router, not a replacement for understanding the declarative API, so this lesson teaches the declarative form first and only mentions framework mode as something you'll encounter later.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark