Nearby lessons
34 of 43🛣️ React Router – Dynamic Routes with useParams
📌 What is useParams?
What is useParams Hook in React Router?
In a React app, sometimes we want to access the parameters
of the current route (like id, username, etc.).
In this case, the useParams hook comes into action.
The useParams hook is part of react-router-dom.
It returns an object of key/value pairs of the dynamic parameters
from the current URL matched by the <Route path>.
📌 What is useParams?
- It is a hook from
react-router-dom. - It returns an object containing dynamic URL parameters.
- We can destructure the params for easy usage.
- Useful for routes like
/user/:id,/product/:pid, etc.
⚙️ Example – Accessing Route Params
Let’s create a dynamic route /user/:id and fetch the id parameter using useParams.
Example03
Loading editor…
🧩 How useParams Works?
- The route path
/user/:idcontains a dynamic segment:id. - When the URL is
/user/101,useParams()returns{ id: "101" }. - We can use this parameter to fetch data, display info, or perform actions.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3