0
0
Reactframework~10 mins

Route parameters in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route parameters
User visits URL with parameter
React Router matches route pattern
Extract parameter from URL
Pass parameter to component
Component uses parameter to render content
When a user visits a URL with a parameter, React Router matches the route, extracts the parameter, and passes it to the component to customize what it shows.
Execution Sample
React
import { useParams } from 'react-router-dom';

function User() {
  const { id } = useParams();
  return <h1>User ID: {id}</h1>;
}

export default User;
This React component gets the 'id' parameter from the URL and shows it on the page.
Execution Table
StepURL VisitedRoute PatternParameter ExtractedComponent Render Output
1/user/42/user/:idid = '42'<h1>User ID: 42</h1>
2/user/abc/user/:idid = 'abc'<h1>User ID: abc</h1>
3/user//user/:idNo matchNo render or fallback
4/user/123/profile/user/:idid = '123'Depends on route config
ExitN/AN/AN/ANo further matching
💡 Execution stops when URL does not match the route pattern or no parameter is found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
idundefined'42''abc'undefineddepends on URL
Key Moments - 2 Insights
Why does the component not render when the URL is '/user/'?
Because the route pattern '/user/:id' expects a parameter after '/user/'. Without it, React Router does not match the route, so the component does not render. See execution_table row 3.
What happens if the URL has extra parts like '/user/123/profile'?
It depends on how routes are configured. If the route matches only '/user/:id', extra parts may prevent matching or require nested routes. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table. What is the value of 'id' when the URL is '/user/abc'?
A'42'
Bundefined
C'abc'
Dnull
💡 Hint
Check execution_table row 2 under 'Parameter Extracted'
At which step does the route fail to match and the component not render?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 where 'No match' is noted
If the URL changes to '/user/99', what will the component render?
ANo render
B<h1>User ID: 99</h1>
C<h1>User ID: 42</h1>
D<h1>User ID: abc</h1>
💡 Hint
Based on variable_tracker, 'id' matches the URL parameter
Concept Snapshot
Route parameters let React Router extract parts of the URL.
Use ':paramName' in route paths to define parameters.
Inside components, use useParams() hook to get parameters.
Parameters are strings from the URL used to customize rendering.
If URL doesn't match pattern, component won't render.
Extra URL parts may affect matching depending on route setup.
Full Transcript
Route parameters in React Router allow parts of the URL to be dynamic. When a user visits a URL like '/user/42', React Router matches the route pattern '/user/:id' and extracts '42' as the 'id' parameter. The component then uses the useParams hook to access 'id' and display it. If the URL lacks the parameter, like '/user/', the route does not match and the component does not render. Extra URL parts may require additional route configuration. This visual trace shows how the parameter changes with different URLs and how the component output updates accordingly.