1.0.1 • Published 6 years ago

use-lazy-loading v1.0.1

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

Overview

A simple, unopinionated React hook for lazy-loading.

NOTE: This library uses Interaction Observer API so you will need to use a polyfill to support older browsers.

Usage

import { useLazyLoading } from 'use-lazy-loading'

const { lazyLoad } = useLazyLoading(target => {
    target.setAttribute('src', target.getAttribute('data-src'))
    target.classList.add('loaded')
})
React.useEffect(() => {
  lazyLoad()
}, [])

Initialization

useLazyLoading(entryCallback, target, config)

Arguments

nametyperequireddefaultdescription
entryCallbackFunctionyesA function that is invoked once the target becomes visible. The function should accept the parameter target that is the element being operated on.
targetStringno[data-src]The query selector used to find the element that needs to be loaded.
configObjectno{threshold: 1.0}The configurations used to instantiate the intersection observer object.

API

Methods

lazyLoad()

Creates the Intersection Observer and binds the scroll handler.

1.0.1

6 years ago

1.0.0

6 years ago