Facebook Pixel

Pagination Component in React

Building a reusable pagination component in React with props and callbacks.

Pagination Component in React

import React from "react"; function Pagination({ currentPage, totalPages, onPageChange }) { function getPageNumbers() { if (totalPages <= 7) return Array.from({ length: totalPages }, (_, i) => i + 1); if (currentPage <= 4) return [1, 2, 3, 4, 5, "...", totalPages]; if (currentPage >= totalPages - 3) return [1, "...", totalPages - 4, totalPages - 3, totalPages - 2, totalPages - 1, totalPages]; return [1, "...", currentPage - 1, currentPage, currentPage + 1, "...", totalPages]; } const pages = getPageNumbers(); return ( <nav aria-label="Pagination"> <ul className="pagination"> <li> <button onClick={() => onPageChange(currentPage - 1)} disabled={currentPage === 1}> Prev </button> </li> {pages.map((p, i) => ( <li key={i}> {p === "..." ? ( <span aria-hidden="true">...</span> ) : ( <button onClick={() => onPageChange(p)} aria-current={p === currentPage ? "page" : undefined} className={p === currentPage ? "active" : ""} > {p} </button> )} </li> ))} <li> <button onClick={() => onPageChange(currentPage + 1)} disabled={currentPage === totalPages}> Next </button> </li> </ul> </nav> ); }

Usage

function App() { const [page, setPage] = useState(1); const totalPages = 20; return ( <> <DataList page={page} /> <Pagination currentPage={page} totalPages={totalPages} onPageChange={setPage} /> </> ); }

The Takeaway

React pagination: props (currentPage, totalPages, onPageChange), getPageNumbers with ellipsis logic, render buttons with active state and disabled prev/next, ARIA attributes for accessibility. Reusable component that any parent can use by passing the current page and a callback.

Create a component with props: currentPage, totalPages, onPageChange. Calculate page numbers with ellipsis logic. Render prev/next buttons (disabled at boundaries) and page number buttons (active for current). Call onPageChange on click.

currentPage (number), totalPages (number), and onPageChange (callback). This makes the component reusable. The parent manages the current page state and fetches data based on it.

Calculate the page numbers array with '...' for gaps. In the render, check if p === '...' and render a span instead of a button. Use key={i} for React keys. Add aria-hidden='true' to the ellipsis span.

Use the disabled prop: disabled={currentPage === 1} for prev, disabled={currentPage === totalPages} for next. Style .pagination button:disabled with opacity and cursor: not-allowed in CSS.

Call onPageChange(p) on button click. The parent updates the state: const [page, setPage] = useState(1); <Pagination onPageChange={setPage} />. The parent then fetches data for the new page. The pagination component is stateless.

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.