1.0.1 • Published 5 years ago

use-firestore-pagination v1.0.1

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

use-firestore-pagination

A react hook for cumulative pagination of a firebase collection. This means that the query maintains a reference to existing documents when loading more documents.

Install

yarn add use-firestore-pagination

Basic usage

import usePagination from "use-firestore-pagination";
import firebase from "firebase/app";

function Example() {
  const {
    loading,
    loadingError,
    loadingMore,
    loadingMoreError,
    hasMore,
    items,
    loadMore
  } = usePagination(
    firebase
      .firestore()
      .collection("recipes")
      .where("userId", "==", 1)
      .orderBy("updatedAt", "desc"),
    {
      limit: 25
    }
  );

  return (
    <div>
      {loading && <div>Loading...</div>}
      {items.map(item => (
        <div>{item.id}</div>
      ))}
      {hasMore && !loadingMore && <button onClick={loadMore}>Load more</button>}
    </div>
  );
}