0
Challenge · 42Routing & Data
Route Parameters
Coreused in ~80% of apps
One product page, a million products. You don't write a million routes — you write one route with a hole in the URL (/products/:id) and read what fell into that hole.
Core idea. A path segment starting with a colon (/users/:id) is a dynamic segment that matches anything; the matched value is captured by name and read inside the component with the useParams() hook — always as a string.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
app.dev/users/7
matched route: <Route path="/users/:id" element={<User />} />
visit /users/
function User() {
const { id} = useParams();
// id === "7" (always a string)
const user = db.get(id);
if (!user) return <NotFound/>;
return <Profile user={user} />;
}
rendersProfile
M
Milo Vance
Designer
Berlin
user #7
The URL segment "7" is read by useParams() as the string id, and the component looks up record #7.
↑ play a little first, then dig in