← Back to React Course | Chapter 8: Routing | Lesson 4 of 10

URL Parameters

URL parameters are like fill-in-the-blank spots in an address, letting one route show different content depending on what's plugged in, like /users/5 vs /users/9.

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.

Note: You can have multiple dynamic segments in one path, like '/posts/:postId/comments/:commentId'.

Warning: The colon syntax only works in the path string passed to Route — you can't add it anywhere else.

Example: Defining a Dynamic Route Segment

markup
// 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.

Note: Destructure exactly the params you need, like const { id } = useParams(), for clarity.

Warning: useParams() always returns strings, even for numeric-looking IDs — use Number(id) or parseInt if you need to compare it numerically.

Example: Reading the Parameter with useParams

markup
// 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.

Note: Include the param in useEffect's dependency array, so navigating from /users/1 to /users/2 re-fetches the new user's data.

Warning: Forgetting the param in the dependency array means the component won't update its data when only the ID in the URL changes.

Example: Using Params to Fetch the Right Data

markup
// 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.

Common Mistakes
  1. Forgetting the colon in the route definition (path='/users/:id' not '/users/id').
  2. Not converting the param string to a number when comparing it against numeric IDs, since useParams always returns strings.
  3. Assuming a param is always present — a route might render before navigation fully resolves it.
Chapter Summary
  • 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.
Browser Support

Requires npm install react-router-dom — not available via CDN in this sandbox.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.