This hook returns a pagination range based on the current page, total number of items, and page size.
Usage
Import the usePagination hook in your React component as follows:
import { usePagination } from './usePagination';
Then, you can use the hook in your component as follows:
const paginationRange = usePagination({
totalCount: 100, // total number of items
pageSize: 10, // number of items per page
siblingCount: 1, // number of siblings to show on either side of the current page
currentPage: 5, // current page
});
The usePagination hook uses the useMemo hook to memoize the pagination range based on the dependencies passed to it. The function takes an object with the following properties:
totalCount - total number of items
pageSize - number of items per page
siblingCount - number of siblings to show on either side of the current page (default value is 1)
currentPage - current page
The pagination range is calculated based on the total number of pages, the current page, and the number of siblings to show on either side of the current page. If the number of pages is less than or equal to the number of pages to be displayed, then all the pages are displayed. Otherwise, the pagination range is calculated based on the current page and the number of siblings to show on either side of it. Dots are added to indicate gaps in the pagination range.
The DOTS constant is defined as ..., and the range function is defined as a utility function to create a range of numbers between start and end inclusive.
The usePagination hook takes an object with the following parameters:
totalCount - total number of items
pageSize - number of items per page
siblingCount - number of siblings to show on either side of the current page (default value is 1)
currentPage - current page