use-pagination-pull
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

use Hook Pagination pull | Hook

npm

The usePagination hook is a custom hook to handle pagination in React applications. With it, you can generate a pagination sequence and navigate between pages.

Documentation storybook

storybook

Stacks

Using hook

const { currentPage, handlePageChange, nextPage, prevPage, range } =
  usePagination({
    initialPage: 1,
    totalPages: 20,
  });

// output;
// range => [1, '...', 3, 4, 5, '...', 20]

 handlePageChange(3)

// currentPage => 3
// range => [1, '...', 2, 3, 4, '...', 20]

Example in React component

import usePagination from "use-pagination";

const { currentPage, handlePageChange, nextPage, prevPage, range } =
  usePagination({
    initialPage: 1,
    totalPages: 20,
  });

<div className="pagination">
  <button onClick={prevPage}>Previous</button>
  {range.map((page, i) => {
    if (typeof page === "string") {
      return (
        <span key={i} className="page">
          {page}
        </span>
      );
    }
    return (
      <button
        key={i}
        className={page === currentPage ? "page active" : "page"}
        onClick={() => handlePageChange(page)}
      >
        {page}
      </button>
    );
  })}
  <button onClick={nextPage}>Next</button>
</div>;

Props | hook usePagination

Props Type Description
totalPages number The total number of pages
initialPage number The initial page
boundary string The boundary
onPageChange func The callback function
modeInfinite bool The infinite mode
hiddenBoundary bool The hidden boundary

Packages Status [100% 🔃]

This is a hook created with Rollup.

npm

Changelog and Roadmap

  • [x] Documentation
  • [x] Testing
  • [x] Initial release

Development

To get running locally:

npm install
npm run storybook

# or

npm run dev

Testing

npm run test

Contributing

Please see our contributing guide

License

MIT License

Readme

Keywords

Package Sidebar

Install

npm i use-pagination-pull

Weekly Downloads

0

Version

1.0.8

License

MIT

Unpacked Size

7.01 kB

Total Files

3

Last publish

Collaborators

  • orriker