URL Parameters
In this page:
<Route path="/items/:id" element={<ItemDetail />} />
function ItemDetail() {
const { id } = useParams();
return <p>{id}</p>;
}
Defining a Dynamic Route Segment
Prefixing a path segment with a colon, like path='/users/:id', tells React Router that segment is a variable, not a literal string. Any URL matching that shape, like /users/5 or /users/42, will match this single route definition.
Example: Defining a Dynamic Route Segment
// Run in your local React project (npm install required)
import { Routes, Route } from 'react-router-dom';
function UserDetail() { return <p>Showing a user</p>; }
function AppRoutes() {
return (
<Routes>
<Route path="/users/:id" element={<UserDetail />} />
</Routes>
);
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
Reading the Parameter with useParams
Inside the component rendered by a dynamic route, calling useParams() returns an object whose keys match the colon-prefixed names in the path. For path='/users/:id', useParams() returns something like { id: 5 } when the URL is /users/5.
Example: Reading the Parameter with useParams
// Run in your local React project (npm install required)
import { useParams } from 'react-router-dom';
function UserDetail() {
const { id } = useParams();
return <p>Showing user with ID: {id}</p>;
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
Using Params to Fetch the Right Data
The most common use of a route param is looking up the matching item from a list or an API using that ID. Since useParams() gives you the current ID directly, you can use it inside useEffect to fetch or find the corresponding data whenever it changes.
Example: Using Params to Fetch the Right Data
// Run in your local React project (npm install required)
import { useParams } from 'react-router-dom';
import { useEffect, useState } from 'react';
function UserDetail() {
const { id } = useParams();
const [name, setName] = useState("");
useEffect(() => { setName("User #" + id); }, [id]);
return <p>{name}</p>;
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
- Forgetting the colon in the route definition (path='/users/:id' not '/users/id').
- Not converting the param string to a number when comparing it against numeric IDs, since useParams always returns strings.
- Assuming a param is always present — a route might render before navigation fully resolves it.
- A route path segment prefixed with a colon (like :id) becomes a dynamic parameter.
- useParams() returns an object of all matched parameters as strings, keyed by their names.
- Params let one Route definition serve many different URLs (e.g. every /users/:id).
- Always parse/convert param strings if you need to compare them against numbers.
Requires npm install react-router-dom — not available via CDN in this sandbox.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: