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

react-use-paginator

v1.3.2

Published

A simple yet configurable react hook for pagination. Functionally developed with hooks.

Downloads

20

Readme

usePaginator React Hook

Declarative Pagination.

A simple yet configurable react hook for pagination. No dependencies. Functionally developed.

usePaginator allows the rapid development of paginated views that are light and performant. Just as easy as it is to get started, there are also plenty of customization options should you require them.

NPM JavaScript Style Guide

Demos

Handful of usage demos can be found on the usage guide.

Install

npm install --save react-use-paginator

Usage

import React from 'react';
import usePaginator from 'react-use-paginator';

const Page = ({ items, index }) => {
  return (
    <div>
      <div>This is page {index}</div>
      <div>It contains the following items:</div>
      {items.map((item) => (
        <div>{item}</div>
      ))}
    </div>
  );
};

const App = () => {
  const data = ['Foo1', 'Bar1', 'Foo2', 'Bar2', 'Foo3', 'Bar3', 'Foo4', 'Bar4'];
  const { Component, nextPage, prevPage } = usePaginator({
    PageComponent: Page,
    maxPerPage: 5,
    data,
  });

  return (
    <div>
      <Button onClick={prevPage}>Prev</Button>
      <Button onClick={nextPage}>Next</Button>
      <Component />
    </div>
  );
};

Input Arguments

const input = {...}
const output = usePaginator(input)

| Option | Type | Description | | --------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | PageComponent | Functional component with signature: (props) => <div/> | A react component to be used in rendering specific page contents. Component will receive index and items as input props in addition to any props you specify when rendering the output Component | | data | Array | Input data that needs to be paginated. The hook will split the data into items per page made available to the PageComponent | | maxPerPage | Number | Default: 25. Optional to be set. Will split the input data into as many pages as there is data. Ex: 80 items in data would have 4 total pages to render. 3 pages of 25 items and a last page of 5 items |

Output Values

The output of the hook is an array of values to use in order to render your component exactly how you need it.

const { ...output } = usePaginator(input);

| Option | Type | Description | | ----------------------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | currentPage | Number | The current page index being shown. Starts at 1. | | Component | React Component | This is the input PageComponent with a specific page's items made available through props. Simply render with <Component /> anywhere on your page. | | hasNextPage | Boolean | Either true or false depending if there is another page to render | | onNextPage Alias: nextPage | Function signature () => {...} | Set the page index directly to the next page. A shortcut to prop into most buttons simply with onClick={nextPage}. This function will automatically roll around to page 1 when called on the last page of results. | | onPrevPage Alias: prevPage | Function signature () => {...} | Set the page index directly to the previous page. A shortcut to prop into most buttons simply with onClick={prevPage}. This function will automatically roll around to the last page when called on the first page of results. | | pageItems | Array | The items from the input data currently being rendered | | setPageIndex | Function signature (index) => {...} | Set the page index directly for use cases where you need to traverse directly to certain pages. This "index" value starts at 1 because all pages do as well. No numbers less than 1 are acceptable | | totalPages | Number | The total number of pages. Essentially this is the ceiling integer value of: data.length / maxPerPage. But why calculate that yourself when the hook has it available already? |

License

MIT © albchu🍍