1.1.5 • Published 4 years ago

react-use-infinite-loading v1.1.5

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

react-use-infinite-loading

Infinite scroll hook for React

NPM JavaScript Style Guide

DEMO

https://evankazadaiev.github.io/use-infinite-loading-hook/

Install

NPM 
npm install --save react-use-infinite-loading
-------------
YARN
yarn add react-use-infinite-loading

Options

OptionTypeDescriptionRequiredDefault value
callbackFunctionapi requesttrue-
hasMoreBooleanif server has more items for ustrue-
startPageNumberStart pagefalse1
offsetNumberDistance for sending request before end of scrolling containerfalse250
directionString // top or bottomScroll directionfalsebottom

Usage

import React from 'react'
import useInfiniteLoading from 'react-use-infinite-loading'


const Example = () => {
  const [pokemons, setPokemons] = useState(null);
  
  const getImages = (page) => new Promise(async (resolve) => {
    const res = await getPokemons(page, 100);
    setPokemons(prev => (prev ? [...prev, ...res] : res));
  
    resolve(res);
  });
  

  const [ref, containerRef, isLoading] = useInfiniteScroll({
    hasMore: true, // if server-side has more items for us
    offset: 100, // send request 100px before the end of scrolling container
    direction: 'bottom', // scroll direction
    callback: getImages, // api request
  });

  return (
    <div ref={containerRef}>
      <ul className="gallery">
        {pokemons &&
        pokemons.map((pokemon, idx) => (
            <Child {...pokemon} key={idx}/>
          ))}
      </ul>
      <Loader ref={ref}>{ isLoading && <Spinner color="goldenrod" size="64px" thickness={2} />}</Loader>
    </div>
  );
};

License

MIT © evankazadaiev