upf

use-pagination-firestore

πŸ”₯ React hook for non-cumulative pagination of Firebase Firestore collections

Showing:

Popularity

Downloads/wk

207

GitHub Stars

11

Maintenance

Last Commit

1mo ago

Contributors

4

Package

Dependencies

0

Size (min+gzip)

1.0KB

License

ISC

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

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.

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 firebase from "firebase/app";

const RecentPerfumes = () => {
    const {
        items,
        isLoading,
        isStart,
        isEnd,
        getPrev,
        getNext,
    } = usePagination<Perfume>(
        firebase
            .firestore()
            .collection("/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>(
        firebase
            .firestore()
            .collection("/perfumes")
            .orderBy("updated", order),
        {
            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.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100