Facebook Pixel

Pagination Common Mistakes and Fixes

Common pagination mistakes and how to fix each one.

Pagination Common Mistakes and Fixes

Mistake 1: Not Clamping the Page Number

Problem: going to page 0 or page total+1.

Fix: currentPage = Math.max(1, Math.min(page, totalPages)).

Mistake 2: No Ellipsis for Many Pages

Problem: 100 page buttons rendered.

Fix: implement the ellipsis algorithm (show first, last, current, neighbors).

Mistake 3: Not Resetting Page on Filter Change

Problem: user is on page 5, applies a filter with only 2 pages, sees nothing.

Fix: reset currentPage = 1 when filters change.

Mistake 4: No Disabled State for Prev/Next

Problem: clicking Prev on page 1 or Next on the last page.

Fix: disable Prev when currentPage === 1 and Next when currentPage === totalPages.

Mistake 5: Not Updating Total Pages on Data Change

Problem: data changes but totalPages is stale.

Fix: recalculate totalPages = Math.ceil(data.length / pageSize) on every data change.

Mistake 6: No Loading State for Server-Side

Problem: blank list while fetching.

Fix: show a loading indicator while the API request is in flight.

The Takeaway

Common mistakes: not clamping page (Math.max/min), no ellipsis (use algorithm), not resetting on filter change (set page to 1), no disabled states (disable at boundaries), stale totalPages (recalculate), and no loading state for server-side. Fix each for a robust pagination component.

Not clamping the page number, no ellipsis for many pages, not resetting page on filter change, no disabled state for prev/next at boundaries, not updating total pages on data change, and no loading state for server-side pagination.

Because the filtered data may have fewer pages. If the user is on page 5 and the filter only has 2 pages, they see nothing. Reset currentPage to 1 when filters change to ensure the user sees valid data.

Clamp the page number: currentPage = Math.max(1, Math.min(requestedPage, totalPages)). This ensures the page is always between 1 and the total number of pages.

Disable prev when currentPage === 1 and next when currentPage === totalPages. Use the disabled attribute: <button disabled={currentPage === 1}>Prev</button>. Style disabled buttons with reduced opacity.

Set isLoading to true before fetching, false after. Show a loading indicator (spinner or 'Loading...') while isLoading is true. Hide the data list while loading. Show the data and pagination when loading completes.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.