Skip to content

Commit 337f71e

Browse files
Kerrickdanieljcafonso
authored andcommitted
Update load.mdx
1 parent 21519d6 commit 337f71e

File tree

1 file changed

+51
-2
lines changed
  • src/routes/solid-router/reference/load-functions

1 file changed

+51
-2
lines changed

src/routes/solid-router/reference/load-functions/load.mdx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,54 @@ const User = lazy(() => import("/pages/users/[id].js"));
4242
<Route path="/users/:id" component={User} load={loadUser} />;
4343
```
4444

45-
The return value of the `load` function is passed to the page component when called at anytime other than `preload`.
46-
This initializes things in there, or alternatively the following new Data APIs can be used:
45+
## Accessing loaded data in the component
46+
47+
### `props.data`
48+
49+
The return value of the `load` function is passed to the page component as a `data` prop when called at anytime other than `preload`.
50+
51+
```js
52+
//pages/users/[id].jsx
53+
export default function User(props) {
54+
return <p>{ props.data().name }</p>
55+
}
56+
```
57+
58+
### Data APIs
59+
60+
Alternatively, the following new Data APIs can be used:
61+
62+
- [`cache`](https://docs.solidjs.com/solid-router/reference/data-apis/cache)
63+
- [`createAsync`](https://docs.solidjs.com/solid-router/reference/data-apis/create-async)
64+
65+
```js
66+
import { lazy } from "solid-js";
67+
import { Route } from "@solidjs/router";
68+
import { loadUser } from "./pages/users/[id].data.js";
69+
70+
const User = lazy(() => import("./pages/users/[id].js"));
71+
72+
// Pass it in the route definition
73+
<Route path="/users/:id" component={User} load={loadUser} />;
74+
```
75+
76+
```js
77+
//pages/users/[id].data.js
78+
79+
import { cache } from "@solidjs/router";
80+
81+
export const getUser = cache((id) => { /* do loading */ }, "users");
82+
export const loadUser = ({ params, location }) => fetchUser(params.id);
83+
export default loadUser;
84+
```
85+
86+
```js
87+
//pages/users/[id].jsx
88+
89+
import { getUser } from "./[id].data.js";
90+
91+
export default function User(props) {
92+
const user = createAsync(() => getUser(props.params.id));
93+
return <p>{ user().name }</p>
94+
}
95+
```

0 commit comments

Comments
 (0)