Route parameters let your app get information from the URL. This helps show different content based on what the user clicks or types.
Route parameters in React
import { useParams } from 'react-router-dom'; function Component() { const params = useParams(); // Access params.id or other keys return <div>{params.id}</div>; }
useParams is a React hook from react-router-dom that reads parameters from the URL.
Route parameters are defined in the route path with a colon, like /user/:id.
id parameter from the URL and shows it.import { useParams } from 'react-router-dom'; function User() { const { id } = useParams(); return <h1>User ID: {id}</h1>; }
productId parameter to display product details.import { useParams } from 'react-router-dom'; function Product() { const { productId } = useParams(); return <p>Product number: {productId}</p>; }
import { useParams } from 'react-router-dom'; function Post() { const params = useParams(); return <div>Post slug: {params.slug}</div>; }
This app has a home page with links to two user profiles. Clicking a link changes the URL with a user ID. The UserProfile component reads that ID from the URL and shows it.
import React from 'react'; import { BrowserRouter as Router, Routes, Route, Link, useParams } from 'react-router-dom'; function UserProfile() { const { userId } = useParams(); return <h2>User Profile for ID: {userId}</h2>; } function Home() { return ( <div> <h1>Home</h1> <nav> <ul> <li><Link to="/user/101">User 101</Link></li> <li><Link to="/user/202">User 202</Link></li> </ul> </nav> </div> ); } export default function App() { return ( <Router> <Routes> <Route path="/" element={<Home />} /> <Route path="/user/:userId" element={<UserProfile />} /> </Routes> </Router> ); }
Route parameters are always strings. Convert them if you need numbers.
If a parameter is missing, useParams() returns undefined for it.
Use semantic HTML and accessible links for better user experience.
Route parameters let your app read values from the URL to show dynamic content.
Use useParams() hook inside components to get these values.
Define parameters in route paths with a colon, like /user/:id.
Practice
useParams() hook in React Router do?Solution
Step 1: Understand the purpose of
This hook is designed to extract parameters defined in the route path from the URL.useParams()Step 2: Compare with other options
Options A, C, and D describe different React Router features, notuseParams().Final Answer:
It retrieves the dynamic parameters from the current URL. -> Option BQuick Check:
useParams() = get URL parameters [OK]
- Confusing useParams() with navigation functions like useNavigate()
- Thinking useParams() sets or changes routes
- Assuming useParams() returns all routes instead of parameters
postId in React Router?Solution
Step 1: Identify parameter syntax in route paths
React Router uses a colon:before the parameter name to define dynamic segments.Step 2: Check each option
<Route path="/post/:postId" element={<Post />} />correctly uses/:postId. Options A, B, and D use incorrect syntax.Final Answer:
<Route path="/post/:postId" element={<Post />} /> -> Option AQuick Check:
Parameter syntax = colon before name [OK]
- Omitting the colon before parameter name
- Using curly braces or asterisk instead of colon
- Confusing parameter syntax with query strings
<Route path="/profile/:username" element={<Profile />} /> and the URL /profile/jane_doe, what will useParams() return inside the Profile component?Solution
Step 1: Match URL segment to route parameter
The route defines:usernameas a parameter. The URL/profile/jane_doeassigns "jane_doe" to this parameter.Step 2: Understand useParams() output
useParams() returns an object with keys as parameter names and values as the matched URL parts.Final Answer:
{"username": "jane_doe"} -> Option CQuick Check:
useParams() returns {paramName: value} [OK]
- Including colon in parameter value
- Using wrong parameter name as key
- Expecting empty object when parameter exists
const params = useParams();
console.log(params.id);
But the route is defined as
<Route path="/product/:productId" element={<Product />} />. What is the problem?Solution
Step 1: Compare parameter names in route and code
The route defines parameter asproductId, but code accessesparams.id.Step 2: Understand useParams() object keys
useParams() keys exactly match route parameter names, soparams.idis undefined here.Final Answer:
The parameter name used in useParams() does not match the route parameter name. -> Option AQuick Check:
Param names must match exactly [OK]
- Assuming useParams() keys are generic like 'id'
- Thinking useParams() works outside components
- Misunderstanding route parameter syntax
/user/42/post/7. Which route path and useParams() usage correctly extract both parameters inside the component?Solution
Step 1: Define route with multiple parameters
The route path correctly uses:userIdand:postIdto capture both values.Step 2: Correctly extract parameters inside component
useParams() returns an object, so destructuring with matching keys{ userId, postId }is correct. Options A, B, and C misuse keys or destructuring.Final Answer:
<Route path="/user/:userId/post/:postId" element={<UserPost />} /> with const { userId, postId } = useParams(); -> Option DQuick Check:
Multiple params = multiple keys in useParams() object [OK]
- Using wrong keys when destructuring useParams()
- Trying to destructure useParams() as an array
- Accessing non-existent keys from useParams()
Ready to prove it?
Pass the test and this pattern moves unknown
