1.0.5 • Published 2 years ago

react-scroll-load-hook v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

react-scroll-load-hook

What is this

The package is similar to other react hook package like: react-infinite-scroll-hook. But implemented by scroll event.

The hook only responsible for detecting the timing to do something (loadMore). You need to handle some status data by yourself, like whether the data is fetching(isLoading), is there has more data, etc.

Implementation

Check the threshold position by your setting, such as use window or not, threshold value. Then, invoke the callback when scroll scrolled to the threshold position by detecting scroll events.

Install

npm i react-scroll-load-hook

Basic usage

import useScrollLoad from "react-scroll-load-hook";
import useLoadList from "./useLoadList";

const ListPage = () => {
  // please handle the state data by yourself.
  const { listData, isLoading, loadMore, hasMore } = useLoadList();

  const { containerRef } = useScrollLoad({
    onLoadMore: loadMore,
    isLoading,
    hasMore,
    useWindow: false,
  });

  return (
    <div ref={containerRef}>
      {listData.map((value, index) => {
        //....
      })}
      {isLoading && <div> loading...</div>}
    </div>
  );
};

ContainerRef will be ignored if useWindow equals true.

import useScrollLoad from "react-scroll-load-hook";
import useLoadList from "./useLoadList";

const ListPage = () => {
  // please handle the state data by yourself.
  const { listData, isLoading, loadMore, hasMore } = useLoadList();

  const { containerRef } = useScrollLoad({
    onLoadMore: loadMore,
    isLoading,
    hasMore,
    useWindow: true,
  });

  return (
    // the div is not the detector because useWindow: true.
    <div ref={containerRef}>
      {listData.map((value, index) => {
        //....
      })}
      {isLoading && <div> loading...</div>}
    </div>
  );
};

Arguments

NameDescriptionTypeOptionalDefault Value
onLoadMoreInvoked when scroll detector reach the threshold position.function
hasMoreIf the list has more items to load.boolean
isLoadingPrevent invoke onLoadMore from checking if something is loading.boolean
axisVertial scroll or horizontal scroll. (x, y)string'y'
useWindowUse window as list scroller or DOM elementHTMLElementfalse
thresholdRange for adjusting the detection area.Number100
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago