Overview
In modern web applications, efficiently displaying large datasets is critical for user experience and performance. For an e-commerce platform, users need to browse a vast product catalog without overwhelming their browser or waiting for massive data loads. This problem focuses on building a robust pagination component for a product listing page, simulating asynchronous data fetching. Your solution will demonstrate proficiency in React state management, UI rendering, and handling common pagination challenges.
What you need to do / Task
Your primary goal is to implement a functional and user-friendly paginated product display.
- Product Listing: Display a list of products retrieved from a simulated asynchronous API call. Each product should show at least its
id and name.
- Pagination Controls: Implement "Previous", "Next", and numbered page buttons.
- Clicking "Previous" or "Next" should navigate to the adjacent page, if available.
- Clicking a numbered page button should navigate directly to that page.
- "Previous" and "Next" buttons should be disabled when at the first or last page, respectively.
- Dynamic Page Number Range: The pagination controls should intelligently display a limited range of page numbers. For example, show the first page, the last page, and a few pages around the current page (e.g.,
1 ... 4 5 [6] 7 8 ... 100). Use ellipses (...) to indicate skipped page ranges. The exact range (e.g., 5-7 visible page numbers) is up to your discretion but should be reasonable.
- Items Per Page Selector: Provide a dropdown or similar control to allow users to change the number of products displayed per page. When this value changes, the product list should update, and the pagination should reset to the first page.
- Loading State: Display a clear loading indicator (e.g., "Loading products...") while data is being fetched.
UI/UX expectations
- Products should be displayed in a clear, scrollable area.
- Pagination controls should be located below the product list.
- The currently active page number button should be visually distinct (e.g., highlighted).
- The "Items per page" selector should be easily accessible, ideally near the pagination controls.
- Loading state should be noticeable and prevent interaction with controls until data is loaded.
Acceptance criteria
- The application loads and displays the first page of products with the default "items per page" setting.
- Clicking "Next" correctly fetches and displays the subsequent page's products.
- Clicking "Previous" correctly fetches and displays the prior page's products.
- Clicking a specific numbered page button navigates directly to that page.
- "Previous" button is disabled on the first page; "Next" button is disabled on the last page.
- The page number display dynamically adjusts, showing the current page, a few surrounding pages, and ellipses for skipped ranges.
- Changing the "items per page" value updates the display, re-fetches data, and resets the current page to 1.
- A loading indicator is visible while product data is being fetched.