Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thejohnfreeman/loadable
Like react-loadable, but better.
https://github.com/thejohnfreeman/loadable
Last synced: 2 months ago
JSON representation
Like react-loadable, but better.
- Host: GitHub
- URL: https://github.com/thejohnfreeman/loadable
- Owner: thejohnfreeman
- License: mit
- Created: 2019-01-25T19:35:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-12T15:56:15.000Z (almost 6 years ago)
- Last Synced: 2024-04-27T07:32:40.424Z (8 months ago)
- Language: TypeScript
- Size: 24.4 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# loadable
Like [`react-loadable`](https://github.com/jamiebuilds/react-loadable), but
better.[![npm](https://img.shields.io/npm/v/@thejohnfreeman/loadable.svg)](https://www.npmjs.com/package/@thejohnfreeman/loadable)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@thejohnfreeman/loadable.svg?style=flat)](https://bundlephobia.com/result?p=@thejohnfreeman/loadable)
[![code style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat)](https://github.com/prettier/prettier)
[![build status](https://travis-ci.org/thejohnfreeman/loadable.svg?branch=master)](https://travis-ci.org/thejohnfreeman/loadable)## Motivation
`react-loadable` has some problems. As of this writing, there are 39 open pull
requests and issues are closed. The creator and maintainer seems to have
abandoned the project, perhaps in favor of
[`@loadable/component`](https://www.smooth-code.com/open-source/loadable-components/),
but perhaps because of [his emotional instability](https://archive.fo/0ZYam).The general idea of `react-loadable` is that you give it an async function,
`load`, that returns a component type (not a component or element), `Target`,
and it will give you a new component type, `Proxy`. The first time an element
of `Proxy` is rendered, it will save its props, call `load`, and return
a placeholder "loading" element while it waits. Once `load` returns with
`Target`, the proxy component will update itself with an element of type
`Target`, passing along the props it saved.What I wanted initially, was the ability to use the props in my `load`
function, but [even `@loadable/component` has
that](https://www.smooth-code.com/open-source/loadable-components/docs/dynamic-import/).
What I want now is to pass an async function that takes the props and returns
an element, not just the component type. Then I can use it for fetching
resources, not just for code-splitting.## Usage
[Code splitting](https://developers.google.com/web/fundamentals/performance/optimizing-javascript/code-splitting/):
```typescript
import loadable from '@thejohnfreeman/loadable'
const Proxy = loadable()(() => import('./Target'))
//
```Resource loading:
```typescript
import loadable from '@thejohnfreeman/loadable'
const AsyncProduct = loadable()(async ({ productId }) => {
try {
const product = await backend.getProduct(productId)
} catch (error) {
return
}
return
})
//
```## Options
Options are passed to the call to `loadable`, not to the function it returns,
which takes the `load` function.- `delay :: number`
Number of milliseconds to wait before showing the placeholder (to avoid
a flicker of content). The default is 200.- `Placeholder :: React.ComponentType`
The placeholder component type to show while waiting for the target. It will
be given the same props as the target, in case you want to make use of them.
The default is the text "Loading...".