# loadable-hook

> React hook for loadable

Latest version **1.0.0** (published 2018-11-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install loadable-hook
pnpm add loadable-hook
yarn add loadable-hook
bun add loadable-hook
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-11-08 |
| First published | 2018-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Jack Hanford |
| Maintainers | hanford |
| Keywords | react, hooks, async, function |

## Links

- npm: https://www.npmjs.com/package/loadable-hook
- Repository: https://github.com/hanford/loadable-hook
- Homepage: https://github.com/hanford/loadable-hook#readme
- Issues: https://github.com/hanford/loadable-hook/issues
- npm.io page: https://npm.io/package/loadable-hook

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-11-08

## README

# `loadable-hook`

> React hook for Loadable

> **Note:** This is using the new [React Hooks API Proposal](https://reactjs.org/docs/hooks-intro.html)
> which is subject to change until React 16.7 final.
>
> You'll need to install `react`, `react-dom`, etc at `^16.7.0-alpha.0`

## Install

```sh
yarn add loadable-hook
```

## Usage

```js
const sleep = time => () => new Promise(done => setTimeout(done, time));

function App() {
  const [{ loading, error }, onClick] = useLoadable(sleep(500));

  return (
    <React.Fragment>
      <pre>
        error: {JSON.stringify(error)}
        <br />
        loading: {JSON.stringify(loading)}
      </pre>
      <button onClick={onClick}>{loading ? "Loading..." : "Load"}</button>
    </React.Fragment>
  );
}
```

### Using delayMs
Sometimes async functions will be cached and the function will return too quickly resulting in a flicker. To mitigate that, you can pass an optional `{ delayMs }` argument to `useLoadable`

```js
const sleep = () => new Promise(done => done());

function App() {
  // this will take atleast 300ms to resolve
  const [{ loading, error }, onClick] = useLoadable(sleep, { delayMs: 300 });

  return (
    <button onClick={onClick}>{loading ? "Loading..." : "Load"}</button>
  );
}
```

[![Edit React Hooks Demo](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/mo8x74m66j)

---
_Source: https://npm.io/package/loadable-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
