Full answer
Pagination is an exercise in derived state. The only value that changes is the page. Everything else is a calculation: page count, the start and end index, and the items to show.
With page numbers starting at 1, the slice starts at (page - 1) * pageSize and ends at page * pageSize. The page count is Math.ceil(items.length / pageSize). Twenty-five items with a page size of ten give three pages, and the last page has five items.
Edge cases make the difference in an interview. Disable Previous on page 1 and Next on the last page, so state can never leave the valid range. When the list changes length, for example after filtering, the current page may no longer exist, so clamp it with Math.min(page, pageCount) or reset to 1. An empty list should show an empty state and "Page 1 of 1" rather than "Page 1 of 0".
Show where the user is: "Page 2 of 3" and the range "11 to 20 of 25". Give the nav element an aria-label and keep button text meaningful.
Client-side slicing is fine for hundreds of items. For thousands or remote data, request one page from the API (?page=2&limit=10), keep the page in the URL's search params so links and the back button work, and use a query library with keepPreviousData behaviour to avoid flashing a blank list between pages. For a feed that grows, use infinite scroll instead.
Putting the page into a state variable and deriving the rest avoids bugs where the stored slice and the page disagree.
import { useState } from 'react';
const students = Array.from({ length: 25 }, (_, i) => 'Student ' + (i + 1));
const PAGE_SIZE = 10;
export default function PagedList() {
const [page, setPage] = useState(1);
const pageCount = Math.max(1, Math.ceil(students.length / PAGE_SIZE));
const safePage = Math.min(page, pageCount);
const start = (safePage - 1) * PAGE_SIZE;
const visible = students.slice(start, start + PAGE_SIZE);
return (
<div>
<ul>
{visible.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
<nav aria-label="Pagination">
<button onClick={() => setPage(safePage - 1)} disabled={safePage === 1}>
Previous
</button>
<button onClick={() => setPage(safePage + 1)} disabled={safePage === pageCount}>
Next
</button>
</nav>
<p>
Page {safePage} of {pageCount}: showing {start + 1} to {start + visible.length} of {students.length}
</p>
</div>
);
}The check pressed Next twice (reaching the short last page), a third time (the button is disabled, so nothing changes), and then Previous. Output of the run: start: Student 1Student 2Student 3Student 4Student 5Student 6Student 7Student 8Student 9Student 10 | Page 1 of 3: showing 1 to 10 of 25 after two Next: Student 21Student 22Student 23Student 24Student 25 | Page 3 of 3: showing 21 to 25 of 25 after a third Next: Student 21Student 22Student 23Student 24Student 25 | Page 3 of 3: showing 21 to 25 of 25 after Previous: Student 11Student 12Student 13Student 14Student 15Student 16Student 17Student 18Student 19Student 20 | Page 2 of 3: showing 11 to 20 of 25
Key points
- Store only the page. Derive page count and visible items.
- Slice from (page - 1) * pageSize to page * pageSize.
- Disable the buttons at the edges and clamp the page when the list shrinks.
- Handle the empty list explicitly.
- For remote data, fetch per page and keep the page in the URL.
Common mistakes
- Using 0-based and 1-based page numbers inconsistently.
- Allowing Next on the last page and showing an empty page.
- Storing the sliced list in state.
- Not clamping the page after the data shrinks.
- Showing "Page 1 of 0" for an empty list.
Follow-ups
- How would you move this to server-side pagination with a query library?
- How would you keep the page in the URL?
- When would you choose infinite scroll over pages?
Tips
- Show the arithmetic for slice and page count out loud.
- Name the edge cases (first page, last page, empty list) before the interviewer asks.
- Mention URL state for shareable links.