Skip to content

Commit 7937b09

Browse files
committed
more content on lazy loading and nested components
1 parent a4c79c1 commit 7937b09

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

src/routes/reference/component-apis/lazy.mdx

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,75 @@
22
title: lazy
33
---
44

5-
```ts
5+
Used to lazy load components to allow for code splitting.
6+
Components are not loaded until rendered.
7+
8+
```tsx title="app.tsx"
9+
import { lazy } from "solid-js"
10+
11+
const ComponentA = lazy(() => import("./ComponentA"));
12+
13+
function App(props: { title: string }) {
14+
return (
15+
<ComponentA title={props.title} />
16+
)
17+
}
18+
```
19+
20+
Lazy loaded components can be used the same as its statically imported counterpart, receiving props etc.
21+
Lazy components trigger `<Suspense>`
22+
23+
## Preloading data in Nested Lazy Components
24+
25+
Top-level lazy components will automatically be preloaded as well as their preload functions.
26+
Though nested lazy components will not be preloaded automatically because they are not part of the route hyerarchy.
27+
To preload such components, you can use the `preload` method exposed on the lazy component
28+
29+
```tsx title="component-with-preload.tsx"
630
import { lazy } from "solid-js"
731
import type { Component } from "solid-js"
832

33+
const Nested = lazy(() => import("./Nested"))
34+
35+
const ComponentWithPreload: Component = () => {
36+
// preload Nested component when needed
37+
async function handlePreload() {
38+
await Nested.preload()
39+
}
40+
41+
return (
42+
<div>
43+
<button onClick={handlePreload}>Preload Nested Component</button>
44+
<Nested />
45+
</div>
46+
)
47+
}
48+
49+
```
50+
51+
## Type Signature
52+
53+
```tsx
954
function lazy<T extends Component<any>>(
1055
fn: () => Promise<{ default: T }>
1156
): T & { preload: () => Promise<T> }
1257
```
1358

14-
Used to lazy load components to allow for code splitting.
15-
Components are not loaded until rendered.
16-
Lazy loaded components can be used the same as its statically imported counterpart, receiving props etc.
17-
Lazy components trigger `<Suspense>`
59+
### Type Parameters
1860

19-
```tsx
20-
// wrap import
21-
const ComponentA = lazy(() => import("./ComponentA"));
61+
| Name | Constraint | Description |
62+
| ---- | ---------- | ----------- |
63+
| `T` | `Component<any>` | The component type that will be lazily loaded (including its props).
64+
65+
### Parameters
66+
67+
| Parameter | Type | Required | Description |
68+
| --------- | ---- | -------- | ----------- |
69+
| `fn` | `() => Promise<{ default: T }>` | Yes | A function that returns a dynamic import resolving to the component as the `default` export. |
70+
71+
### Returns
72+
73+
| Type | Description |
74+
| ---- | ----------- |
75+
| `T & { preload: () => Promise<T> }` | A renderable component compatible with `T` that also exposes a `preload()` method to eagerly load the module. |
2276

23-
// use in JSX
24-
<ComponentA title={props.title} />
25-
```

src/routes/solid-router/advanced-concepts/preloading.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ This helper will load only the route's component by default, but it can receive
1919

2020
## Preloading and Lazy Loading
2121

22-
When a route has nested lazy components, such components will not be part of the route hierarchy, so they **will not** be preloaded with the route. To preload such components, you can use the [`usePreloadRoute`](/solid-router/reference/primitives/use-preload-route) helper in the parent component to load them when needed.
22+
When a route has nested lazy components, such components will not be part of the route hierarchy, so they **will not** be preloaded with the route. To preload such components, you can use the [`usePreloadRoute`](/solid-router/reference/primitives/use-preload-route) helper in the parent component to load them when needed.
23+
24+
To learn more about lazy loading components, see the [`lazy`](/reference/component-apis/lazy#preloading-data-in-nested-lazy-components) documentation.

0 commit comments

Comments
 (0)