Load or display a resource by ID
A component renders content for a dynamic URL such as /posts/$postId.
TanStack · Hook
useParams returns parsed path parameters from the closest route match and its parents. Options can scope the route or select a derived result.
useParams reads path parameters from the closest matched route and its parent matches. You can constrain it to a route with from, derive a smaller value with select, or use strict: false when accessing parameters from an ambiguous location.
useParams<TSelected = TAllParams>(opts?: { from?: TFrom; select?: (params: TAllParams) => TSelected; strict?: boolean; structuralSharing?: boolean }): TSelected| Name | Type | Description |
|---|---|---|
opts | object | Optional route and selection configuration, including from, select, strict, and structuralSharing. |
The matched route and parent path-parameter object, or the value returned by select.
import { createRoot } from 'react-dom/client'
import { createRouter, RouterProvider, Route } from '@tanstack/react-router'
const postRoute = new Route({
getParentRoute: () => rootRoute,
path: '/posts/$postId',
component: Post,
})
function Post() {
const params = postRoute.useParams()
return <p>Post: {params.postId}</p>
}
const router = createRouter({ routeTree: rootRoute.addChildren([postRoute]) })
createRoot(document.getElementById('root')!).render(<RouterProvider router={router} />)The route-bound hook reads the typed postId value from the /posts/$postId path.
A component renders content for a dynamic URL such as /posts/$postId.
A shared component needs route parameters without a single statically known route.
A component needs one parameter rather than the complete parameter object.
useParams returns the path parameters parsed for the closest match together with parameters from its parent matches. The result is the complete parameter object unless select transforms it.
Follow-up: How would you read only one parameter? Pass select, such as select: params => params.postId, and use the returned selected value.
Use the from option to identify the route whose parameters should be read, for example { from: '/posts/$postId' }. This gives the hook route-specific type information.
Follow-up: What if the component is not associated with one route? Use strict: false to access parameters from an ambiguous location.
The hook accepts an optional select function that receives the parameter object and returns a derived value. That returned value becomes the hook result and participates in shallow-equality re-render checks.
Follow-up: When should select be used? Use it when a component needs only a subset or computed view of the route parameters.
Path ParamsRouteApiRoute.useParamsuseSearchuseLoaderData