Skip to content

Commit 6af0ad5

Browse files
Sanjai451davidbieliktimdorr
authored
Updated Documentation with examples using props (#12838)
Co-authored-by: davidbielik <[email protected]> Co-authored-by: Tim Dorr <[email protected]>
1 parent f219a17 commit 6af0ad5

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

contributors.yml

+1
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,4 @@
340340
- yuleicul
341341
- zeromask1337
342342
- zheng-chuang
343+
- sanjai451

docs/start/framework/data-loading.md

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export async function clientLoader({
135135
params,
136136
}: Route.ClientLoaderArgs) {
137137
const res = await fetch(`/api/products/${params.pid}`);
138+
const serverData = await serverLoader();
138139
return { ...serverData, ...res.json() };
139140
}
140141

docs/start/framework/route-module.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This guide is a quick overview of every route module feature. The rest of the ge
2525

2626
## Component (`default`)
2727

28-
Defines the component that will render when the route matches.
28+
The `default` export in a route module defines the component that will render when the route matches.
2929

3030
```tsx filename=app/routes/my-route.tsx
3131
export default function MyRouteComponent() {
@@ -40,6 +40,40 @@ export default function MyRouteComponent() {
4040
}
4141
```
4242

43+
### Props passed to the Component
44+
45+
When the component is rendered, it is provided the props defined in `Route.ComponentProps` that React Router will automatically generate for you. These props include:
46+
47+
1. `loaderData`: The data returned from the `loader` function in this route module
48+
2. `actionData`: The data returned from the `action` function in this route module
49+
3. `params`: An object containing the route parameters (if any).
50+
4. `matches`: An array of all the matches in the current route tree.
51+
52+
You can use these props in place of hooks like `useLoaderData` or `useParams`. This may be preferrable because they will be automatically typed correctly for the route.
53+
54+
### Using props
55+
56+
```tsx filename=app/routes/my-route-with-default-params.tsx
57+
import type { Route } from './+types/route-name'
58+
59+
export default function MyRouteComponent({
60+
loaderData,
61+
actionData,
62+
params,
63+
matches
64+
}: Route.ComponentProps) {
65+
return (
66+
<div>
67+
<h1>Welcome to My Route with Props!</h1>
68+
<p>Loader Data: {JSON.stringify(loaderData)}</p>
69+
<p>Action Data: {JSON.stringify(actionData)}</p>
70+
<p>Route Parameters: {JSON.stringify(params)}</p>
71+
<p>Matched Routes: {JSON.stringify(matches)}</p>
72+
</div>
73+
);
74+
}
75+
```
76+
4377
## `loader`
4478

4579
Route loaders provide data to route components before they are rendered. They are only called on the server when server rendering or during the build with pre-rendering.

0 commit comments

Comments
 (0)