# use-lazy-component

> A React hook to lazily load a component

Latest version **1.0.0** (published 2020-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-lazy-component
pnpm add use-lazy-component
yarn add use-lazy-component
bun add use-lazy-component
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-04-02 |
| First published | 2020-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Tj Broodryk |
| Maintainers | tjbroodryk |
| Keywords | lazy, react, suspense, hook |

## Links

- npm: https://www.npmjs.com/package/use-lazy-component
- Repository: https://github.com/TjaartBroodryk/use-lazy-component
- Homepage: https://github.com/TjaartBroodryk/use-lazy-component#readme
- Issues: https://github.com/TjaartBroodryk/use-lazy-component/issues
- npm.io page: https://npm.io/package/use-lazy-component

## 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) — 2020-04-02
- 0.0.1 — 2020-04-02

## README

[![npm](https://img.shields.io/badge/npm-use--lazy--component-red.svg?style=for-the-badge&logo=npm)](https://www.npmjs.com/package/use-lazy-component)
[![npm](https://img.shields.io/npm/v/use-lazy-component.svg?style=for-the-badge&label)](https://www.npmjs.com/use-lazy-component)

Simple react hook to lazily load a component

- Can be used as a very simplistic alternative to `React.Suspense`

## Installation

```sh
npm i -s use-lazy-component
```
Or
```sh
yarn add use-lazy-component
```

## Usage
Basic usage

```ts
import useLazyComponent from 'use-lazy-component';
```

```jsx padded 
const SomeComponent = () => {
  const { component: Component, loading, error } = useLazyComponent<BigComponent>(
    () => import('./bigComponent');
  );

  return (
    <div>
      {!loading && <Component/> }
    </div>
  )
}
```

 Load different component depending on result of promise
```ts
import useLazyComponent from 'use-lazy-component';
```

```jsx padded 
const SomeComponent = () => {
  const { component: Component, loading, error } = useLazyComponent<BigComponent>(
    async () => {
      const result = await someAsyncFunction();

      if (result) {
        return import('./bigComponent');
      }
      return import('./defaultComponent');
    }
  );

  return (
    <div>
      {!loading && <Component/> }
    </div>
  )
}
```

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