Skip to content

Commit ef9c8b8

Browse files
committed
docs: useParams updates
closes #8865
1 parent a912347 commit ef9c8b8

File tree

1 file changed

+81
-5
lines changed

1 file changed

+81
-5
lines changed

docs/api/hooks/useParams.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,97 @@ title: useParams
1313
Returns an object of key/value pairs of the dynamic params from the current URL that were matched by the routes. Child routes inherit all params from their parent routes.
1414

1515
```tsx
16-
import { useParams } from "react-router"
16+
import { useParams } from "react-router";
1717

1818
function SomeComponent() {
19-
let params = useParams()
20-
params.postId
19+
let params = useParams();
20+
params.postId;
2121
}
2222
```
2323

2424
Assuming a route pattern like `/posts/:postId` is matched by `/posts/123` then `params.postId` will be `"123"`.
2525

26+
## Examples
2627

28+
### Basic Usage
2729

28-
## Signature
30+
```tsx
31+
import { useParams } from "react-router";
32+
33+
// given a route like:
34+
<Route path="/posts/:postId" element={<Post />} />;
35+
36+
// or a data route like:
37+
createBrowserRouter([
38+
{
39+
path: "/posts/:postId",
40+
component: Post,
41+
},
42+
]);
43+
44+
// or in routes.ts
45+
route("/posts/:postId", "routes/post.tsx");
46+
```
47+
48+
Access the params in a component:
49+
50+
```tsx
51+
import { useParams } from "react-router";
52+
53+
export default function Post() {
54+
let params = useParams();
55+
return <h1>Post: {params.postId}</h1>;
56+
}
57+
```
58+
59+
### Multiple Params
60+
61+
Patterns can have multiple params:
62+
63+
```tsx
64+
"/posts/:postId/comments/:commentId";
65+
```
66+
67+
All will be available in the params object:
2968

3069
```tsx
31-
useParams(): Readonly
70+
import { useParams } from "react-router";
71+
72+
export default function Post() {
73+
let params = useParams();
74+
return (
75+
<h1>
76+
Post: {params.postId}, Comment: {params.commentId}
77+
</h1>
78+
);
79+
}
3280
```
3381

82+
### Catchall Params
83+
84+
Catchall params are defined with `*`:
85+
86+
```tsx
87+
"/files/*";
88+
```
89+
90+
The matched value will be available in the params object as follows:
91+
92+
```tsx
93+
import { useParams } from "react-router";
94+
95+
export default function File() {
96+
let params = useParams();
97+
let catchall = params["*"];
98+
// ...
99+
}
100+
```
101+
102+
You can destructure the catchall param:
103+
104+
```tsx
105+
export default function File() {
106+
let { "*": catchall } = useParams();
107+
console.log(catchall);
108+
}
109+
```

0 commit comments

Comments
 (0)