Skip to content

Commit a10a9d5

Browse files
authored
feat: add load method that returns a Promise (#329)
See #226 for context
1 parent 7a9518f commit a10a9d5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/component/src/createLoadable.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ function createLoadable({ resolve = identity, render, onLoad }) {
195195
ctor.requireAsync(props)
196196
}
197197

198+
Loadable.load = props => {
199+
if (typeof window === 'undefined') {
200+
throw new Error('`load` cannot be called server-side')
201+
}
202+
return ctor.requireAsync(props)
203+
}
204+
198205
return Loadable
199206
}
200207

website/src/pages/docs/api-loadable-component.mdx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ A component created using `loadable` or `lazy`.
4848

4949
## LoadableComponent.preload
5050

51-
Force the loading of a component.
51+
Triggers the loading of a component.
5252

5353
| Arguments | Description |
5454
| --------- | ---------------------------------- |
@@ -62,7 +62,27 @@ const OtherComponent = loadable(() => import('./OtherComponent'))
6262
OtherComponent.preload()
6363
```
6464

65-
> This method does not return a Promise intentionnally. See https://github.com/smooth-code/loadable-components/pull/226.
65+
> This method does not return a Promise intentionnally. Use `load` if you need to wait for the component to be loaded.
66+
67+
## LoadableComponent.load
68+
69+
Force the loading of a component synchronously, returns a Promise.
70+
71+
| Arguments | Description |
72+
| --------- | ---------------------------------- |
73+
| `args` | Props passed to the load function. |
74+
75+
```js
76+
import loadable from '@loadable/component'
77+
78+
const OtherComponent = loadable(() => import('./OtherComponent'))
79+
80+
OtherComponent.load().then(() => {
81+
console.log('Component is loaded!')
82+
})
83+
```
84+
85+
> If you don't need to know when the component will be loaded, you should use `preload` instead.
6686
6787
## loadable.lib
6888

0 commit comments

Comments
 (0)