Skip to main content

Overview

Tienda ETCA implements pagination for the product gallery using React-Bootstrap’s Pagination component. The system displays 5 products per page and includes smooth scrolling to the top of the gallery when changing pages.

Implementation

Pagination is implemented in the ProductList component at ~/workspace/source/src/components/ProductList.jsx:5-70.

Import

State Management

The pagination state tracks the current page number, starting from 1.

Pagination Logic

The component calculates which products to display based on the current page:

Calculation Breakdown

1

Calculate Last Index

For page 1: 1 * 5 = 5
2

Calculate First Index

For page 1: 5 - 5 = 0
3

Slice Products Array

For page 1: productosFiltrados.slice(0, 5) returns items 0-4
4

Calculate Total Pages

For 23 products: Math.ceil(23 / 5) = 5 pages

Pagination UI

The pagination component renders at ~/workspace/source/src/components/ProductList.jsx:50-70:

Component Features

Previous Button

Decrements page by 1
Disabled on page 1

Page Numbers

Dynamically generated based on total pages
Active page highlighted

Next Button

Increments page by 1
Disabled on last page

Smooth Scrolling

When users navigate between pages, the gallery automatically scrolls to the top for better UX:
The isFirstRender check prevents scrolling on initial page load, only scrolling when users actively change pages.

Scroll Target

The gallery container is marked with a ref:
Pagination automatically adapts to search results. When users search for products, the pagination recalculates based on filtered results:
Shows: Page 1, 2, 3, 4, 5

Full Component Code

Complete implementation from ~/workspace/source/src/components/ProductList.jsx:7-72:

Customization Options

Change Items Per Page

Custom Page Button Styling

Best Practices

Use Math.max() and Math.min() to ensure page numbers stay within valid range:
Disable Previous button on first page and Next button on last page for better UX:
Skip scrolling on initial render to prevent jarring page load:
Always calculate total pages based on filtered results for search compatibility:
For large product catalogs with many pages, consider implementing page number ellipsis (e.g., “1 … 5 6 7 … 20”) to keep the pagination UI clean.