h3/use's logo

h3/use

Command Palette

Search for a command to run...

usePagination

Hook for controlling list pagination.

usePagination
Example of pagination using the usePagination hook.

Page: 1 of 10

Items per page: 10

Installation

Props

PropTypeDefaultDescription
totalItemsnumber-Total number of items to paginate
initialPagenumber1Initial page
initialPageSizenumber10Number of items per page

Data

PropertyTypeDescription
pagenumberCurrent page
pageSizenumberNumber of items per page
totalPagesnumberTotal number of pages
setPage(page: number) => voidDefine the current page
nextPage() => voidGo to the next page
prevPage() => voidGo to the previous page
setPageSize(size: number) => voidDefine the number of items per page

Key Features & Details

  • Manages page and pageSize state.
  • Automatically calculates the total number of pages.
  • Ensures the current page stays within valid bounds.
  • Supports dynamic changes to pageSize.

Examples

Basic Usage

const { page, pageSize, totalPages, setPage, nextPage, prevPage, setPageSize } =
  usePagination(100);
 
return (
  <div>
    <p>
      Page {page} of {totalPages}
    </p>
    <button onClick={prevPage} disabled={page <= 1}>
      Previous
    </button>
    <button onClick={nextPage} disabled={page >= totalPages}>
      Next
    </button>
  </div>
);

Custom Page Size

const { page, pageSize, setPageSize } = usePagination(200, {
  initialPageSize: 20,
});