Skip to content

🔥 React hook for non-cumulative pagination of Firebase Firestore collections

Notifications You must be signed in to change notification settings

premshree/use-pagination-firestore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-pagination-firestore

A React Hook that makes it easy to paginate firestore collections. This hook is similar, but the not the same as firestore-pagination-hook. This hook provides non cumulative pagination and does not maintain references to previous documents, so it might be suitable for large document sets.

Support for Firebase 9

Versions 0.6.x add support for Firebase 9 and are backwards incompatible with previous versions of Firebase. For support for Firebase 8.x, use versions 5.x of use-pagination-firestore.

Install

npm install use-pagination-firestore

Example Use

This is an example of a "recently added perfumes" section built using Material UI and Firestore. You can see it live on the Petrichor homepage here, or here is a screencast.

import React from 'react';
import Grid from "@material-ui/core/Grid";
import PerfumeCard from "./search/PerfumeCard";
import { usePagination } from "use-pagination-firestore";
import Loading from "./Loading";
import {
    NavigateNext as NavgateNextIcon,
    NavigateBefore as NavigateBeforeIcon
} from '@material-ui/icons';
import { IconButton } from "@material-ui/core";
import { initializeApp } from "firebase/app";
import { getFirestore, collection, query, orderBy } from "firebase/firestore";

const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);

const RecentPerfumes = () => {
    const {
        items,
        isLoading,
        isStart,
        isEnd,
        getPrev,
        getNext,
    } = usePagination<Perfume>(
        query(collection(db, "/perfumes"), orderBy("updated", "desc")),
        {
            limit: 10
        }
    );

    if (isLoading) {
        return <Loading/>;
    }

    return (
        <Grid container>
            <Grid item xs={12}>
                <Grid container justify="flex-end">
                    <Grid item>
                        <IconButton onClick={getPrev} disabled={isStart}>
                            <NavigateBeforeIcon/>
                        </IconButton>
                        <IconButton onClick={getNext} disabled={isEnd}>
                            <NavgateNextIcon/>
                        </IconButton>
                    </Grid>
                </Grid>
            </Grid>
            {items.map((perfume, idx) => {
                return (
                    <Grid item xs={12} sm={12} md={6} lg={6} key={`recent-perfume-${idx}`}>
                        <PerfumeCard perfume={perfume} size="medium"/>
                    </Grid>
                );
            })}
        </Grid>
    );
}

export default RecentPerfumes;

You can also change query during runtime. Hook will detect new query and start pagination from the beginning. Here is an example of controlling query's limit and orderDirection by React's state:

type ORDER_DIRECTION = 'asc' | 'desc';
const DEFAULT_PAGE_SIZE = 10;

const RecentPerfumes = () => {
    const [pageSize, setPageSize] = useState(DEFAULT_PAGE_SIZE);
    const [order, setOrder] = useState<ORDER_DIRECTION>('desc');
    const {
        items,
        isLoading,
        isStart,
        isEnd,
        getPrev,
        getNext,
    } = usePagination<Perfume>(
        query(collection(db, "/perfumes"), orderBy("updated", "desc")),
        {
            limit: pageSize
        }
    );

    if (isLoading) {
        return <Loading/>;
    }

    return (
        <Grid container>
            <Grid item xs={12}>
                <Grid container justify="flex-end">
                    <Grid item>
                        <PageSizeSelect pageSize={pageSize} onChange={setPageSize} />
                        <OrderDirectionSelect order={order} onChange={setOrder} />
                        <IconButton onClick={getPrev} disabled={isStart}>
                            <NavigateBeforeIcon/>
                        </IconButton>
                        <IconButton onClick={getNext} disabled={isEnd}>
                            <NavgateNextIcon/>
                        </IconButton>
                    </Grid>
                </Grid>
            </Grid>
            {items.map((perfume, idx) => {
                return (
                    <Grid item xs={12} sm={12} md={6} lg={6} key={`recent-perfume-${idx}`}>
                        <PerfumeCard perfume={perfume} size="medium"/>
                    </Grid>
                );
            })}
        </Grid>
    );
}

Caveats

Paginating Firestore documents relies on query cursors. It's not easy to know ahead of time how many documents exist in a collection. Consequently, if your document_count % page_size is 0 you will notice that your last page is empty – this is because this hook doesn't (currently) look ahead to know if there are any more documents.

About

🔥 React hook for non-cumulative pagination of Firebase Firestore collections

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published