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-paginated

v1.0.0-rc.2

Published

use-paginated library provide a helpful react hook for implementing pagination

Downloads

1

Readme

use-paginated

npm Build Status Coverage Status

use-paginated library provides a helpful react hook for implementing pagination logic in React.

This is not a UI component, if you want a library for rendering a UI pagination bar you can think of react-js-pagination which by the way can be used easily with use-paginated library.

This library uses React hooks and local state, so any loaded pages data will be stored localy in the component. In the upcoming versions of this library we may add support for storing the data in a redux store...

Install

npm install --save use-paginated

Usage

Very easy to use. Just provide the options to the hook usePagination and you will get everything you need to render and change between pages.

function usePagination<T>(options: UsePaginationOptions<T>): UsePaginationProps<T>

interface UsePaginationOptions<T> {
  fetchPage: (page: number, maxPerPage: number) => Promise<FetchPageResponse<T>> | FetchPageResponse<T>;
  maxPerPage?: number;
  caching?: boolean;
  defaultPage?: number;
}

maxPerPage: is the number of items per page, you can change it using changeMaxPerPage.

caching: default to true, to disable or enable caching of loaded data in order to optimize the number of fething requests.

defaultPage: default to 1, is the default page to load when the component mounts.

fetchPage: is a function that should load the data (from a source...) it should return a promise or simple object of type FetchPageResponse<T> containing the page items and the total number of elements.

interface FetchPageResponse<T> {
  pageItems: T[];
  totalCount: number;
}
interface UsePaginationProps<T> {
  currentPage: number;
  loadingStatus: UsePaginationStatus; // The status of loading the current page
  loadingStatusMessage?: string; // A message error when loading the page failed
  changePage: (page: number) => void; // to change current page
  nextPage: () => void; // To load next page
  previousPage: () => void; // to load previous page
  currentPageItems: T[]; // The content of the current page, that should be displayed
  changeMaxPage: (maxPage: number) => void;
  maxPerPage: number;
  totalCount: number; // Total number of items
  totalPages: number; // Number of pages
}

Example

import React, { useState } from "react";
import usePagination, { FetchPageResponse } from "use-paginated";

type User = {
  id: number;
  email: string;
  first_name: string;
  last_name: string;
  avatar: string;
};
function adaptFakeData(data: any): FetchPageResponse<User> {
  return {
    pageItems: data.data,
    totalCount: data.total,
  };
}
const fetchPage = (page: number, maxPerPage: number) => {
    // This is a fake api I found in the net
  return fetch(
    `https://reqres.in/api/users?page=${page}&per_page=${maxPerPage}`
  )
    .then((response) => response.json())
    .then((data) => adaptFakeData(data));
};

function App() {
  const [page, setPage] = useState(1);
  const [perPage, setPerPage] = useState(3);
  const {
    totalCount,
    currentPage,
    maxPerPage,
    currentPageItems,
    loadingStatus,
    loadingStatusMessage,
    changePage,
    nextPage,
    previousPage,
    changeMaxPerPage,
    totalPages,
  } = usePagination({
    fetchPage,
    caching: true,
    defaultPage: 1,
    maxPerPage: perPage
  });
  const onPerPageChange = (event: any) =>{
    if(event.currentTarget.value && event.currentTarget.value !== "")
      setPerPage(parseInt(event.currentTarget.value))
  }
  const onPageChange = (event: any) =>{
    if(event.currentTarget.value && event.currentTarget.value !== "")
      setPage(parseInt(event.currentTarget.value))
  }
  return <div>
      <pre>
        { JSON.stringify(currentPageItems, null, 2 )}
      </pre>
      <p>{"Current page:"} {currentPage}</p>
      <p>{"Loading status:"} {loadingStatus}</p>
      <p>{"Loading status message:"} {loadingStatusMessage}</p>
      <p>{"Total count:"} {totalCount}</p>
      <p>{"Max per page:"} {maxPerPage}</p>
      <p>{"Number of pages:"} {totalPages}</p>
      <button onClick={previousPage} disabled={currentPage === 1}> {"Previous"} </button>
      <button onClick={nextPage} disabled={currentPage === totalPages}> {"Next"} </button>
      <input type="number" value={page} onChange={onPageChange} />
      <button onClick={() => changePage(page)} disabled={page > totalPages || page < 1}> {"Go to this page"} </button>
      <input type="number" value={perPage} onChange={onPerPageChange} />
      <button onClick={() => changeMaxPerPage(perPage)}> {"Change max per page"} </button>

  </div>;
}

export default App;

Feedback

If you have any feedback or interested in imporving this library, please send a PR or post an issue.