Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/saitolume/use-lazyload-ref

🖼️ Custom hook to use lazyload easily
https://github.com/saitolume/use-lazyload-ref

custom-hook lazyload react

Last synced: 13 days ago
JSON representation

🖼️ Custom hook to use lazyload easily

Awesome Lists containing this project

README

        

# use-lazyload-ref

[![GitHub license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/saitoeku3/use-lazyload-ref/blob/master/LICENSE) [![npm version](https://badge.fury.io/js/use-lazyload-ref.svg)](https://badge.fury.io/js/use-lazyload-ref) ![CI Status](https://github.com/saitoeku3/use-lazyload-ref/workflows/Node%20CI/badge.svg)

> Custom hook to use lazyload easily

## Installation

```
$ npm install use-lazyload-ref
$ yarn add use-lazyload-ref
```

## Usage

To enable lazy loading, simply specify `ref` and `data-src` attributes.

```jsx
import React from 'react'
import useLazyloadRef from 'use-lazyload-ref'

const Component = ({ url }) => {
const [ref, hasLoaded] = useLazyloadRef()

return (


{hasLoaded || }

{/* */}
{/* */}
{/* */}
{/*
*/}

)
}
```

## API

### `useLazyloadRef: () => [(node: HTMLElement | null) => void, boolean]`

Custom hook.
In addition to `src` in `audio`, `iframe`, `img` and `video`, CSS property `background-image` can be lazy loaded.

```jsx
const [ref, hasLoaded] = useLazyloadRef()
```

`useLazyloadRef` returns a callback ref function and a load state.

- `ref`: Initialize observer for lazyload.
- `hasLoaded`: Default is false. Turn true if the source has finished loading.

## License

MIT