@leejaehyeok/use-pagination
v0.1.0
Published
A React hook for building flexible pagination UI with customizable ranges, ellipsis, and both controlled and uncontrolled modes.
Downloads
70
Maintainers
Readme
@leejaehyeok/use-pagination
A React hook for building flexible pagination UI with customizable page ranges, ellipsis, and navigation controls. Supports both controlled and uncontrolled modes.
📦 Installation
npm install @leejaehyeok/use-pagination🚀 Quick Start
The hook returns pagination data and navigation methods to build a pagination component.
import React, { useState } from "react";
import { usePagination } from "@leejaehyeok/use-pagination";
export default function Pagination() {
const { page, totalPages, setPage, handleNext, handlePrevious, paginationRange, isFirstPage, isLastPage } = usePagination({
totalItems: 100,
itemsPerPage: 10,
siblings: 1,
boundaries: 1,
defaultPage: 1,
});
return (
<div>
<p>
Current Page: {page} / {totalPages}
</p>
<button onClick={handlePrevious} disabled={isFirstPage}>
Previous
</button>
{paginationRange.map((item) =>
item.type === "page" ? (
<button key={item.key} onClick={() => setPage(item.page)} style={{ fontWeight: page === item.page ? "bold" : "normal" }}>
{item.page}
</button>
) : (
<span key={item.key}>...</span>
),
)}
<button onClick={handleNext} disabled={isLastPage}>
Next
</button>
</div>
);
}🧠 Key Features
- Flexible Range: Customize
siblings(pages around current) andboundaries(pages at start/end) to create various pagination layouts. - Smart Ellipsis: Automatically shows ellipsis (
...) based on page gaps. - Navigation Methods:
handleNext(),handlePrevious(),handleSkipNext(),handleSkipPrevious(), andsetPage(). - State Helpers:
isFirstPageandisLastPageflags for easy UI control. - Controlled & Uncontrolled: Supports both modes via
currentPage/onPageChangeprops ordefaultPage.
🔗 Links
📄 License
MIT © leejh1316
