0.0.5 • Published 4 years ago

react-use-scroll-more v0.0.5

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

react useScrollMore

This module exports a useScrollMore hook which triggers the fetchMore function when user scroll to the page bottom

Example

import { useScrollMore } from "react-use-scroll-more";

export function App() {
  const { data, fetching, error } = useScrollMore(
    ["apple"],
    async ({ offset }) => {
      const res = await fetch(`/api/fruits?offset=${offset}&limit=10`);
      return res.json();
    }
  );
  if (fetching) {
    return "fetching fruits...";
  }
  if (error) {
    return "error...";
  }
  return (
    <ul>
      {data.map((fruit) => (
        <li key={fruit}>{fruit}</li>
      ))}
    </ul>
  );
}

Interface

type FetchMoreArgs = {
  offset: number;
};

type ScrollMoreOptions = {
  timeout?: number;
};

type FetchMoreFn<T> = (option: FetchMoreArgs) => Promise<T[]> | T[];

function useScrollMore<T>(
  init: T[],
  fetchMore: FetchMoreFn<T>,
  options?: ScrollMoreOptions
): {
  fetching: boolean;
  done: boolean;
  error: boolean;
  data: T[];
};
0.0.5

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.4

4 years ago

0.0.1

4 years ago