usePagination
A hook that handles pagination logic with page navigation and item display.
usePagination
The usePagination
hook simplifies pagination logic in React applications. It handles page navigation, boundary detection, and generates accessible pagination controls for your content, whether you’re paginating a list, table, or any other content that needs to be split across multiple pages.
Installation
Install the usePagination hook using:
File Structure
Parameters
Prop | Type | Default |
---|---|---|
totalItems | number | Required |
pageSize | number | Required |
siblingCount | number | 1 |
currentPage | number | 1 |
Return Value
Prop | Type | Default |
---|---|---|
currentPage | number | - |
totalPages | number | - |
pageSize | number | - |
pageItems | (number | -1)[] | - |
firstPage | () => void | - |
previousPage | () => void | - |
nextPage | () => void | - |
lastPage | () => void | - |
goToPage | (page: number) => void | - |
canPreviousPage | boolean | - |
canNextPage | boolean | - |
Examples
Table Pagination
Create a paginated table with controls for changing page size:
ID | Name | Price | Stock |
---|---|---|---|
1 | Product 1 | $97 | 4 |
2 | Product 2 | $43 | 45 |
3 | Product 3 | $21 | 16 |
4 | Product 4 | $79 | 0 |
5 | Product 5 | $10 | 48 |
API Data Pagination
Use the hook with async data fetching:
Custom Page Size Controls
Allow users to control items per page:
Use Cases
- Data Tables: Paginate rows of tabular data
- Search Results: Display search results across multiple pages
- Product Listings: Show products or items in pages
- Comments & Reviews: Paginate user-generated content
- Blog Posts: Navigate through blog post archives
- Image Galleries: Display images across multiple pages
- Admin Dashboards: Paginate lists of users, orders, or other resources
- API Data: Handle pagination for data fetched from APIs
Algorithm
The usePagination
hook uses a smart algorithm for generating the page numbers to display:
- If the total number of pages is small, it shows all page numbers
- If there are many pages, it shows:
- First page
- Ellipsis (…) if needed
- A few pages around the current page (based on
siblingCount
) - Ellipsis (…) if needed
- Last page
This approach keeps the pagination controls compact while maintaining good usability.
Accessibility
When implementing pagination with this hook, consider these accessibility best practices:
- Add proper
aria-label
attributes to pagination buttons - Include a screen reader-accessible text describing the current page status
- Ensure keyboard navigation works correctly
- Maintain focus when changing pages
- Consider adding
aria-current="page"
to the current page button
URL Integration
You can integrate the pagination state with the URL for shareable pages:
Best Practices
- Maintain a reasonable
pageSize
based on your content and UI design - Consider loading states during async data fetching
- Provide visual indication of the current page
- Include first/last page buttons for large datasets
- Offer a way to adjust the number of items per page for user preference
- Keep pagination controls in a consistent location
- Use ellipsis (…) for large page ranges to maintain a clean UI
- Consider “back to top” functionality when changing pages