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-combined-pagination

v0.0.11

Published

A React Hook for paginating data from multiple sources 🦑

Downloads

7

Readme

use-combined-pagination 🦑

NPM eslint formatted eslint formatted

use-combined-pagination is a React hook for paginating across multiple data sources at once, whilst retaining the sort order.

  • Great for Infinity Scroll: easily support multiple data sources in your infinity scroll.
  • Retain order: your data is always in order, even when it comes from different sources.
  • Service agnostic: work with any data service, whether REST, GraphQL or another.
  • Mix-and-match services: mix data services as you see fit, making one query from GraphQL and one from a REST API.
  • Efficient: only fetch data when needed for that data source.

This is a refactor of the combine-pagination module made by @chrisvxd.

Install

npm install --save use-combined-pagination

or with Yarn:

yarn add use-combined-pagination

Basic Usage

This module is meant to be used for merge and sort multiple data sources at once, before start using this module, please read the problem that it solves.

import React, { Component } from 'react'
import { useCombinedPagination } from 'use-combined-pagination'

const MyDataList = () => {
  const fetchData1 = async (page: number) => {
    // ... fetch data from data source 1
  }

  const fetchData2 = async (page: number) => {
    // ... fetch data from data source 2
  }

  const { loading, data, getNext, hasNext } = useCombinedPagination({
    getters: [fetchData1, fetchData2],
    sortKey: 'popularity'
  })

  useEffect(() => {
    // fetch the first page on mount
    getNext()
  }, [])

  return (
    <div>
      {loading && <div>Loading...</div>}
      {data && data.map((item) => <div key={item.id}>{item.name}</div>)}
      {hasNext && (
        <button disabled={loading} onClick={getNext}>
          Load more
        </button>
      )}
    </div>
  )
}

getNext() Usage

getNext() function is used to fetch the next page of data.

It returns a promise that resolves with an array of sorted data.

const { getNext } = useCombinedPagination({
  getters: [fetchData1, fetchData2],
  sortKey: 'popularity'
})

// ...

const page1 = await getNext()
const page2 = await getNext()
const page3 = await getNext()

refetch() Usage

refetch() function is used to reset the data and refetch the first page.

const { getNext, refetch, data } = useCombinedPagination({
  getters: [fetchData1, fetchData2],
  sortKey: 'popularity'
})

// ...

return (
  <>
    <button onClick={refetch}>Refetch</button>
    <MyDataList data={data} />
  </>
)

reset() Usage

reset() function is used to reset the data and the state, causing getNext() to refetch the first page.

const { getNext, reset } = useCombinedPagination({
  getters: [fetchData1, fetchData2],
  sortKey: 'popularity'
})

useEffect(() => {
  reset()
}, [filterParams])

More Information

More information about the problem, the solution, use cases, Framed Range Intersecting and fuzzy pagination can be found on the combine-pagination README.

TODO

  • [ ] CONTRIBUTING Guide
  • [ ] create-react-app example
  • [ ] Add tests for loading
  • [x] Reset functionality
  • [ ] Auto fetch first page on mount
  • [ ] Refetch state and data on params change
  • [ ] Add support for multiple sortKey

Credits

This module is a React Hook + Typescript refactor of the combine-pagination project.

This project would not have existed without the excellent work of @chrisvxd.

License

MIT © Hyperting S.r.l.