Skip to content

Commit

Permalink
feat: add load method that returns a Promise (#329)
Browse files Browse the repository at this point in the history
See #226 for context
  • Loading branch information
gregberge authored May 9, 2019
1 parent 7a9518f commit a10a9d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/component/src/createLoadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ function createLoadable({ resolve = identity, render, onLoad }) {
ctor.requireAsync(props)
}

Loadable.load = props => {
if (typeof window === 'undefined') {
throw new Error('`load` cannot be called server-side')
}
return ctor.requireAsync(props)
}

return Loadable
}

Expand Down
24 changes: 22 additions & 2 deletions website/src/pages/docs/api-loadable-component.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ A component created using `loadable` or `lazy`.

## LoadableComponent.preload

Force the loading of a component.
Triggers the loading of a component.

| Arguments | Description |
| --------- | ---------------------------------- |
Expand All @@ -62,7 +62,27 @@ const OtherComponent = loadable(() => import('./OtherComponent'))
OtherComponent.preload()
```

> This method does not return a Promise intentionnally. See https://github.com/smooth-code/loadable-components/pull/226.
> This method does not return a Promise intentionnally. Use `load` if you need to wait for the component to be loaded.
## LoadableComponent.load

Force the loading of a component synchronously, returns a Promise.

| Arguments | Description |
| --------- | ---------------------------------- |
| `args` | Props passed to the load function. |

```js
import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'))

OtherComponent.load().then(() => {
console.log('Component is loaded!')
})
```

> If you don't need to know when the component will be loaded, you should use `preload` instead.
## loadable.lib

Expand Down

0 comments on commit a10a9d5

Please sign in to comment.