Frontend School

Infinite Scroll Product Catalog

Machine Coding
medium

Infinite Scroll Product Catalog

Medium
Infinite Scroll
React
Frontend
State Management
Hooks
Intersection Observer

Overview

In modern web applications, especially those dealing with large datasets like e-commerce product listings or social media feeds, efficient data loading is paramount. Infinite scrolling is a popular UI/UX pattern that enhances user experience by progressively loading content as the user scrolls down, eliminating the need for traditional pagination. This problem challenges you to implement a robust infinite scroll mechanism for a simulated product catalog.

What you need to do / Task

Your primary goal is to build a React component that displays a list of products and dynamically loads more products as the user scrolls towards the bottom of the page.

  1. Initial Load: Fetch and display the first set of products when the component mounts.
  2. Infinite Scroll Logic: Implement a mechanism to detect when the user has scrolled near the end of the current product list.
  3. Load More Products: When the scroll threshold is met, trigger a function to fetch the next batch of products from a simulated API.
  4. Display Products: Append the newly fetched products to the existing list, ensuring a continuous flow.
  5. Loading Indicator: Show a clear loading indicator (e.g., text or spinner) while new products are being fetched.
  6. End of List: Handle the scenario where all available products have been loaded. Display an appropriate message and stop further loading attempts.

UI/UX expectations

  • Layout: Products should be displayed in a simple grid or list format. Each product card should show at least its name and price.
  • Loading State: A visible message or spinner should appear at the bottom of the list when data is being fetched.
  • End State: A distinct message (e.g., "You've reached the end of the catalog!") should be shown once all products are loaded and there are no more to fetch.
  • Responsiveness: Basic readability on different screen sizes is expected, but complex responsive design is not a focus.

Acceptance criteria

  • The application renders an initial set of products on load.
  • Scrolling down the page triggers the loading of additional products.
  • A loading indicator is displayed only when products are actively being fetched.
  • Newly loaded products are appended to the existing list, without replacing previously loaded items.
  • The loading mechanism correctly identifies when there are no more products to fetch and stops attempting to load further data.
  • An "end of list" message is displayed when all products have been loaded.
  • No duplicate products appear in the list.
  • The UI remains responsive and smooth during scrolling and data loading.

Constraints

Constraints & assumptions

  • You must use React for this implementation, focusing on functional components and hooks.
  • Data fetching should be simulated using the provided fetchProductsApi function, which uses setTimeout to mimic network latency.
  • Each simulated API call will return a fixed number of products (e.g., 15 per page).
  • The total number of available products is finite (e.g., 100 products).
  • You are not allowed to use any third-party infinite scroll libraries (e.g., react-infinite-scroll-component). The infinite scroll logic must be implemented from scratch.
  • The focus is on the core infinite scroll mechanism and state management.

Out of scope

  • Advanced error handling for network failures.
  • Filtering, sorting, or searching capabilities for products.
  • Complex styling, animations, or pixel-perfect UI design.
  • Deep performance optimizations beyond standard React practices (e.g., useCallback, memo) if they naturally arise during implementation.

What interviewers evaluate

  • Correct and efficient implementation of the infinite scroll logic, preferably using the Intersection Observer API.
  • Effective state management for handling product data, loading status, and end-of-list conditions.
  • Clean, modular, and idiomatic React code structure using hooks.
  • Thoughtful handling of UI feedback (loading indicators, end messages) for a good user experience.

Loading editor...