useParams useParams Reads Route Params

Last updated: 2026-11-02

useParams useParams Reads Route Params

useParams returns an object with the dynamic segments of the currently matched route: when a route is written as /users/:id, visiting /users/42 gives you { id: '42' }. Detail pages and edit pages rely on it to know which record they are showing, so the URL alone drives the content. Remember that a param is always a string, so compare it with Number(id) before doing numeric checks.

GroupRouting & State Mgmt
KindHook
SinceReact Router 6.4

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

import { useParams } from 'react-router-dom'

// Route: <Route path="/users/:id" element={<UserDetail />} />
function UserDetail() {
  const { id } = useParams()        // { id: '42' }, note that it is a string
  const user = users.find(u => u.id === id)
  return <h1>User {user?.name}</h1>
}

Returns:An object whose keys are the parameter names declared with : in the route and whose values are the actual URL segments (strings, e.g. { id: '42' }).

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Clicking "Visit /users/2" changes the param to 2 and the page shows Bob - the same effect as useParams reading a dynamic segment of the URL (simulated demo).

💡 Key Points

⚠️ Warning A param is always a string: id === 2 is never true, so call Number(id) first or compare strings consistently.
💡 Tip Optional params use :id?, the splat wildcard uses *, and inside nested routes useParams merges the params of every parent route.
ℹ️ Info To read the query string (?tab=2) use useSearchParams instead.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Does useParams give me a number or a string?
A A string. A URL segment is text by nature, so convert it with Number(id) when you need a numeric value.
Q Does the component remount when the param changes?
A No. Going from /users/1 to /users/2 only changes the params and the component instance is reused, so a data request that depends on the id belongs in an Effect with id in its dependency list.
Q How do I declare several dynamic segments?
A The route /users/:uid/posts/:pid declares several at once and useParams returns { uid, pid }.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏