Routing
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.
| Piece | Job | Example |
|---|---|---|
| Route path | Match a URL pattern | /orders/:id |
| Link | Navigate inside the app | to="/orders/42" |
| useParams | Read the dynamic segment | id 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
- MemoryRouter starts at /orders/42.
- Link points at that same path.
- Route path /orders/:id renders Order.
- Order reads id and shows Order 42.
- 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.
Mental model
FOLLOW THE VALUE
The first match renders.
01 / 03Trace the transformation
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.
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.
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
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>; }
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.