Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 minLESSON 07 / 8

Routing

01

Understand the idea

The URL is part of the user interface. React Router picks which component to show from the path, and it can read values out of that path.

The URL chooses the screen

In a multi-page mental model, /orders/42 is a different page. In a React single-page app, the document often stays loaded, and a router swaps which component tree is shown.

React Router pieces you need first:

  • A router wrapper (this lesson uses MemoryRouter so the example works inside the preview without a real address bar).
  • Routes and Route to declare path → component.
  • Link to move between paths without a full browser reload.
  • useParams to read dynamic pieces like :id.

A deployed app usually uses a browser router so the real URL bar stays in sync. The Route, Link, and useParams ideas stay the same.

Reading /orders/42

A path string like /orders/:id matches /orders/42. Inside the Order component, useParams gives you an object. The id field is the string 42.

It is a string, not a number. If you compare it to the number 42 with strict equality, the check fails. Convert with Number when you need a number.

Call useParams in the component that the matching Route rendered. Outside that route, you will not get the params you expect.

Small router cheat sheet
PieceJobExample
Route pathMatch a URL pattern/orders/:id
LinkNavigate inside the appto="/orders/42"
useParamsRead the dynamic segmentid comes back as a string

Why put the id in the URL

If the order id lives only in , a teammate cannot paste a link that opens the same order. The path /orders/42 is shareable. That is the product reason routing exists beyond “changing screens.”

Keep the first route table small

Start with a flat list of routes. Add nested routes later when a section shares a layout. Do not invent a second “current page” variable that can disagree with the URL.

How to read the live example

  1. MemoryRouter starts at /orders/42.
  2. Link points at that same path.
  3. Route path /orders/:id renders Order.
  4. Order reads id and shows Order 42.
  5. The console logs /orders/42, Link, useParams, 42, and route.

Click the link if you change the path while exploring. Confirm the can render from the param alone.

02

Mental model

FOLLOW THE VALUE

URL
Routes
element

The first match renders.

01 / 03Trace the transformation

03

Live example

The preview starts at /orders/42. Order reads id with useParams and shows Order 42. The console logs the path ideas for this lesson.

import { MemoryRouter, Routes, Route, Link, useParams } from "react-router-dom";
export default function App() {
  return (
    <MemoryRouter initialEntries={["/orders/42"]}> {/* In-memory URL for the preview. */}
      <nav><Link to="/orders/42">Order 42</Link></nav> {/* Navigate without a full reload. */}
      <Routes>
        <Route path="/orders/:id" element={<Order />} /> {/* Match the dynamic segment. */}
      </Routes>
    </MemoryRouter>
  );
}
function Order() {
  const { id } = useParams(); // Read the :id from the path.
  console.log("/orders/42"); // The URL this screen matches.
  console.log("Link"); // Link changes the route in the app.
  console.log("useParams"); // Params come from the path.
  console.log(id); // The id segment is a string.
  console.log("route"); // This component is the matched route.
  return <h1>Order {id}</h1>;
}

VISUAL FLOW

Try the same flow interactively

The path chooses the screen. Dynamic segments become strings you can read with useParams.

Step 1 of 4

Click through URL → match → params

Current path

/orders/42

Start from a path

MemoryRouter initialEntries={["/orders/42"]}

The preview begins at /orders/42. In a real app, that would also be the address bar.

04

Guided challenge

Read the order id

The param log says missing. Change it to 42, the id in /orders/42.

export default function App() {
  console.log("/orders/42"); // Keep the path log.
  console.log("Link"); // Keep the Link log.
  console.log("useParams"); // Keep the useParams log.
  console.log("missing"); // Wrong - change this to 42.
  console.log("route"); // Keep the route log.
  return <p>missing</p>;
}

EXPECTED CONSOLE OUTPUT

/orders/42
Link
useParams
42
route

Hints 0/2

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    import { MemoryRouter, Routes, Route, Link, useParams } from "react-router-dom";
    export default function App() {
      return (
        <MemoryRouter initialEntries={["/orders/42"]}> {/* Try another initial path. */}
          <nav><Link to="/orders/42">Order 42</Link></nav>
          <Routes>
            <Route path="/orders/:id" element={<Order />} />
          </Routes>
        </MemoryRouter>
      );
    }
    function Order() {
      const { id } = useParams(); // Read whatever id is in the URL.
      return <h1>Order {id}</h1>;
    }
    
    
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does useParams return for /orders/42 and path /orders/:id?
    02Why use Link instead of a plain anchor for an in-app path?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson