Routers like React Router and Angular Router manage dynamic URL parameters by allowing you to define placeholders in the route path. These placeholders capture segments of the URL, which can then be accessed in your components to fetch data, display dynamic content, or perform logic based on the URL.
✅ React Router (v6+)
🔹 Defining a Route with Dynamic Parameters
You define a route using :
followed by the parameter name:
This matches URLs like /users/123
or /users/john
.
🔹 Accessing Dynamic Parameters
Use the useParams()
hook inside the component:
You can then use this value to make API calls, fetch data, etc.
✅ Angular Router
🔹 Defining a Route with Dynamic Parameters
In your route configuration (app-routing.module.ts
):
🔹 Accessing Dynamic Parameters
Use ActivatedRoute
in the component:
🧠 What’s Happening Behind the Scenes?
-
Pattern Matching: The router parses the current URL and matches it against route definitions.
-
Parameter Extraction: If a dynamic segment (
:param
) is in the route path, the router extracts the corresponding value from the URL. -
Injection: The router makes these values available to the component—via hooks (
useParams
) in React or services (ActivatedRoute
) in Angular.
✅ Summary Table
Feature | React Router | Angular Router |
---|---|---|
Route Param Syntax | :param | :param |
Access Method | useParams() | ActivatedRoute → paramMap |
Supports Nested Routes | ✅ | ✅ |
Dynamic Route Matching | ✅ | ✅ |