usePagination
React
Press Enter to start editing
const Preview = () => { const [currentPage, setCurrentPage] = useState(2); const { pages, nextButtonProps, prevButtonProps } = usePagination({ totalPages: 10, currentPage, setCurrentPage: setCurrentPage, showPages: 5, }); return ( <Pagination aria-label='Page navigation'> <Pagination.List> <Pagination.Item> <Pagination.Button aria-label='Previous page' {...prevButtonProps}> Previous </Pagination.Button> </Pagination.Item> {pages.map(({ page, itemKey, buttonProps }) => ( <Pagination.Item key={itemKey}> {typeof page === 'number' && ( <Pagination.Button aria-label={`Page ${page}`} {...buttonProps}> {page} </Pagination.Button> )} </Pagination.Item> ))} <Pagination.Item> <Pagination.Button aria-label='Next page' {...nextButtonProps}> Next </Pagination.Button> </Pagination.Item> </Pagination.List> </Pagination> ); }; render(<Preview />)
A React hook to handle pagination.
Props
In response you get the object:
Use
On Pagination.Button
You loop through pages from the response object, and spread buttonProps on Pagination.Button.
Notice that we check typeof on page. This is because ellipses should be an empty li.