npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-pagination-firestore

v0.6.0

Published

A non-cumulative pagination hook for use with Firestore

Downloads

42

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.

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.