|
2 | 2 | title: lazy |
3 | 3 | --- |
4 | 4 |
|
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" |
6 | 30 | import { lazy } from "solid-js" |
7 | 31 | import type { Component } from "solid-js" |
8 | 32 |
|
| 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 |
9 | 54 | function lazy<T extends Component<any>>( |
10 | 55 | fn: () => Promise<{ default: T }> |
11 | 56 | ): T & { preload: () => Promise<T> } |
12 | 57 | ``` |
13 | 58 |
|
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 |
18 | 60 |
|
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. | |
22 | 76 |
|
23 | | -// use in JSX |
24 | | -<ComponentA title={props.title} /> |
25 | | -``` |
|
0 commit comments