# use-lazy-hooks

> react hook for resource lazy loading

Latest version **1.0.1** (published 2019-07-01) · MIT license · 0 weekly downloads

## Install

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

## 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.1 |
| Published | 2019-07-01 |
| First published | 2019-06-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | joe06102 |
| Maintainers | joe06102 |
| Keywords | hooks, lazy-loading, react |

## Links

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

## 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.1 (latest) — 2019-07-01
- 1.0.0 — 2019-06-30

## README

# Use-lazy-hooks
React hook for resource lazy loading.

## Installation

`npm install --save use-lazy-hooks`

## Documentation

The useLazy hook returns an `boolean` value indicating whether the element is in the specific area or not.

1. [Example](#example)
2. [Options](#options)

## <a id="example">Example</a>
Mostly, we'll need an image to be lazy to save the bandwidth and improve the performance. Let's create an image component with `use-lazy` hook.

1. create a ref representing the image dom element that will be mounted.

1. call the use `useLazy` hook with the ref object.

> Both useRef and React.createRef are the same.

```javascript
import React, { useRef } from 'react'
import useLazy from 'use-lazy'

function Img(props) {
  const ref = useRef(null)
  const isInSight = useLazy(ref)

  return <img {...props} ref={ref} src={isInSight ? props.src : ''} />
}
```

Congratulations, you've created a img component that support lazy loading !

## <a id="options">Options</a>

1. **ref**

    Created by `useRef` or `React.createRef`.

1. **coordinate**

    The specific area in which the hook returns `true`. If ignored, the viewport will be the default area.
    **Only `right` & `bottom` prop are supported so far !**

    ```typescript
    interface ICoordinate {
      top?: number,
      right?: number,
      bottom?: number,
      left?: number,
    }
    ```

1. **throttle**

    The handler for scroll/resize throttle. If ignored, the below debounce handler will be used.

    ```typescript
    const debounce = (fn: Function, ctx?: Object, delay: number = 500): EventListener => {
      let timer: number;
      let self: Object = ctx || this;

      return (): void => {
        if (timer) clearTimeout(timer);

        timer = setTimeout((): void => {
          fn.call(self);
        }, delay);
      }
    }    
    ```

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