0.0.2-dev4 • Published 4 years ago

react-use-infinite-scroll-list v0.0.2-dev4

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

react-use-infinite-scroll-list

React hooks - Infinite Scroll List powered by IntersectionObserver

🎊 Install

npm install react-use-infinite-scroll-list

📝 Usage

import useInfiniteScrollList from 'react-use-infinite-scroll-list';

const YOUR_COMPONENT = () => {
  // hooks initialize
  const [items, setItems] = useState([]);
  const { InfiniteScrollList } = useInfiniteScrollList({
    init: init,
    more: more,
    loadingElement: <div>Loading...</div>,
  });

  // init : fetch first data
  async function init() {
    return new Promise((resolve, reject) => {
      YOUR_API()
        .then((res) => {
          // ...
          setItems([res.items]);
          resolve({
            isDone: res.isDone,
          });
        })
        .catch((error) => {
          // ...
          reject(error);
        });
    });
  }

  // more : fetch more data when scrolling to bottom of list
  async function more() {
    return new Promise((resolve, reject) => {
      YOUR_API()
        .then((res) => {
          // ...
          setItems([...items, res.items]);
          resolve({ isDone: res.isDone });
        })
        .catch((error) => {
          // ...
          reject(error);
        });
    });
  }

  return (
    <>
      <InfiniteScrollList>
        <YourListComponent items={items} />
      </InfiniteScrollList>
    </>
  );
};

🔧 Props

  • init : a callback function to fetch first-page data. you should return a Promise resolve with isDone value.
  • more : a callback function to fetch more data when scrolling to the end of list. you should return a Promise resolve with isDone value.
  • loadingElement : (optional) JSX Component to display on a bottom of list while executing more callback
0.0.2-dev3

4 years ago

0.0.2-dev4

4 years ago

0.0.2-dev2

4 years ago

0.0.1-dev6

4 years ago

0.0.2-dev1

4 years ago

0.0.1-dev5

4 years ago

0.0.1-dev4

4 years ago

0.0.1-dev3

4 years ago

0.0.1-dev2

4 years ago

0.0.1-dev1

4 years ago