usePaginate hook provides a complete pagination solution with automatic page management, navigation controls, and integration with the global tweaks store for persistent state.
Signature
Parameters
Array of items to paginate. Can be any type of data.
Number of items per page
Returns
Object containing paginated data and navigation controls:
Usage Example
Basic Pagination
Advanced Examples
With Filtering and Search
Custom Pagination Controls
Pagination with Page Size Selector
How It Works
Slice Calculation
The hook calculates which items to show using array slicing:- Page 1: Items 0-19 (
slice(0, 20)) - Page 2: Items 20-39 (
slice(20, 40)) - Page 3: Items 40-59 (
slice(40, 60))
Page Count
- 100 items, 20 per page = 5 pages
- 95 items, 20 per page = 5 pages (last page has 15 items)
- 20 items, 20 per page = 1 page
- 0 items = 0 pages
Auto-Reset on List Change
When the list changes and current page becomes invalid, it auto-resets:Navigation Functions
next()
Navigates to the next page, capped at the last page:- On page 3 of 5 → Goes to page 4
- On page 5 of 5 → Stays on page 5
- On page 0 (empty list) → Stays on page 0
prev()
Navigates to the previous page, capped at page 1:- On page 3 of 5 → Goes to page 2
- On page 1 → Stays on page 1
setCurrent()
Jump directly to any page:State Persistence
The current page is stored inuseTweaksStore and persists across:
- Component re-renders
- Route navigation within the same session
- Not persisted: Page refreshes (stored in sessionStorage, not localStorage)
- Navigate from page 1 → page 3
- Click on a Pokemon to view details
- Click back button
- Result: Still on page 3 (state preserved)
Edge Cases Handled
Empty List
Current Page Exceeds Total
Invalid Page Numbers
Single Item
Pagination UI Patterns
Pattern 1: Simple Prev/Next
Pattern 2: Page Number Buttons
Pattern 3: Truncated Page Numbers
Pattern 4: With Item Count
Performance Optimization
The hook usesuseMemo to prevent unnecessary recalculations:
- Source list changes
- Current page changes
- Items per page limit changes
- Total page count changes
Complete Example
Related
- usePokeFilters Hook - Filter before paginating
- Tweaks Store - Stores current page state
- Pokemon Service - Provides data to paginate