1.0.1 • Published 5 years ago

@alexcambose/react-infinite-scroll v1.0.1

Weekly downloads
20
License
MIT
Repository
github
Last release
5 years ago

react-infinite-scroll

Build Status

Another react infinite scroll package

Demo and more docs

Installation

npm package

npm i -S @alexcambose/react-infinite-scroll
const InfiniteScroll = require('@alexcambose/react-infinite-scroll');
// or
import InfiniteScroll from '@alexcambose/react-infinite-scroll';

Usage

Basic example

class Basic extends Component {
  state = {
    items: [],
    skip: 0,
    perLoad: 10,
    hasMore: true,
  };

  loadMore = () => {
    const { items, skip, perLoad } = this.state;
    fetch(
      `https://jsonplaceholder.typicode.com/posts?_start=${skip}&_limit=${perLoad}`
    )
      .then(res => res.json())
      .then(res => {
        this.setState({
          items: [...items, ...res],
          skip: skip + perLoad,
          hasMore: res.length >= perLoad,
        });
      });
  };

  render() {
    const { items, hasMore } = this.state;
    return (
      <InifiniteScroll hasMore={hasMore} loadMore={this.loadMore}>
        {items.map((e, i) => (
          <div key={i}>{e.title}</div>
        ))}
      </InifiniteScroll>
    );
  }
}

Props

Property NameisRequireddefaulttypeDescription
loadMoretrue-funcA function that will be called after reaching the bottom. It should fetch data and append it to the current data object. Eg: 1,2,3 -> 1,2,3,4,5. It receives in the first parameter a function that can be called after the fetching is done to check again if the user scroll is below the last piece of content to call the loadMore function again. Eg: loadMore = callback => get('url').then(() => callback())
initialLoadfalsetrueboolIf true it will call loadMore at the beginning, when componentDidMount.
isLoadingfalsefalseboolToggles the loading animation
loadingfalse"Loading..."nodeLoading animation. Can be any valid react element.
hasMoretrue-boolIf there are no more items(skip + limt >= totalCount) this prop should change to false so loadMore is not called again and the `
noMorefalse"No more items."nodeNo more message. Can be any valid react element.
autofalse{}objectEnabled auto mode. See description here.
throttlefalse100numberHow many number of milliseconds need to pass so that the scroll handler will be executed once
scrollableElementfalsewindowinstanceof HTMLElementThe HTML Element that will be used to get and calculate the scroll position.
getScrollTopfalsescrollableElement.scrollYfunctionOverrides the default method of getting the element scroll top position. The single parameter is represented by the scrollableElement.
getElementHeightfalsescrollableElement.offsetHeightfunctionOverrides the default method of getting the element height. The single parameter is represented by the scrollableElement

Auto mode

Auto mode is a feature that lets you create infinite scrolling lists without worrying about handling skip or checking if there are items left to fetch.

Basic example

class Basic extends Component {
  state = {
    items: [],
  };

  loadMore = (skip, perLoad) =>
    fetch(
      `https://jsonplaceholder.typicode.com/posts?_start=${skip}&_limit=${perLoad}`
    ).then(res => res.json());

  render() {
    const { items } = this.state;
    return (
      <InifiniteScroll
        auto={{
          loadMore: this.loadMore,
          perLoad: 10,
          onLoadMore: newItems =>
            this.setState({ items: [...items, ...newItems] }),
        }}
      >
        {items.map((e, i) => (
          <div key={i}>{e.title}</div>
        ))}
      </InifiniteScroll>
    );
  }
}

Props for auto mode

Property NameisRequireddefaulttypeDescription
loadMoretrue-funcFunction that will be called to fetch items. Takes two arguments: skip and perLoad. Must return a promise.
onLoadMoretrue-funcCallback that will be called when loadMore is resolved. Take as an arguments the result from the loadMore.
perLoadtrue-numberHow many items to load at once. Will also be passed as an arguments to loadMore
useCountfalsefalseboolIf it's set to true, infinite scroll will calculate if it has more items based on the total number of items.
countfalse-funcThe function that will be called to get the number of all items. Must return a number. If useCount is set to true and count prop is not specified it will use loadMore with the skip and perLoad set to 0.
onCountfalse-funcCalled after count is resolved. Takes as an argument the items.
onCountErrorfalse-funcCalled if count is rejected. Takes as an argument the promise error.
onLoadMoreErrorfalse-funcCalled if loadMore is rejected. Takes as an argument the promise error.
initialSkipfalse0numberHow many items to skip at the first load.