Frontend School

Infinite Scroll Feed for User Posts

Machine Coding
medium

Infinite Scroll Feed for User Posts

Medium
Infinite Scroll
React
Frontend
UI
Hooks
JavaScript
TypeScript

Overview

In this problem, you will implement an infinite scroll feed, a common pattern in social media and content-heavy applications. This pattern significantly enhances user experience by seamlessly loading more content as the user scrolls, avoiding explicit pagination. Your task is to build a React component that fetches and displays a list of user posts, continuously loading more data from a mock API as the user approaches the bottom of the feed.

What you need to do / Task

  1. Initial Data Load: On component mount, fetch and display the first page of posts.
  2. Infinite Scroll Logic: Implement logic to detect when the user has scrolled near the bottom of the feed container.
  3. Load More Data: When the scroll-to-bottom condition is met, fetch the next page of posts from the mock API and append them to the existing list.
  4. Loading Indicator: Display a clear loading indicator (e.g., "Loading more posts...") while data is being fetched. This indicator should disappear once new data is loaded or if there's no more data to fetch.
  5. End of Feed State: Once all available data has been fetched (i.e., the API returns an empty array or fewer items than requested), display a message indicating the end of the feed (e.g., "You've reached the end of the feed.").

UI/UX expectations

  • Layout: The feed should display posts as a simple vertical list. Each post can be represented by its id and content.
  • Scroll Container: The entire list of posts should be scrollable within a fixed-height container, simulating a typical feed.
  • Loading State: The loading indicator should be visible at the bottom of the feed while new data is being fetched.
  • End State: The "end of feed" message should be visible at the bottom when no more data is available.

Acceptance criteria

  • The component renders initially with the first page of posts.
  • Scrolling down loads the next page of posts.
  • A loading indicator is visible while new data is being fetched.
  • The loading indicator disappears once data is loaded or fetching completes.
  • Scrolling continuously loads more posts until all available data is fetched.
  • A "You've reached the end of the feed." message is displayed when no more data can be loaded.
  • The application does not attempt to fetch data unnecessarily (e.g., not fetching multiple times for a single scroll to bottom event).

Constraints

Constraints & assumptions

  • Mock API: You are provided with a mock fetchPostsPage(page: number, limit: number) function that simulates an asynchronous API call, returning a Promise that resolves with an array of Post objects.
  • Data Structure: Each Post object has at least an id: string | number and content: string.
  • Page Size: The mock API always returns a fixed number of items per page (limit) unless there are fewer items remaining.
  • No External Libraries: You should implement the infinite scroll logic yourself, without relying on third-party infinite scroll libraries (e.g., react-infinite-scroll-component, react-virtualized).
  • Environment: React with TypeScript.
  • Container Height: Assume the feed container has a fixed height and its content overflows, making it scrollable.

Out of scope

  • Advanced error handling (e.g., retry mechanisms, displaying specific error messages beyond a console log).
  • Complex UI styling or animations beyond basic layout and visibility of indicators.
  • Client-side caching of posts.
  • Accessibility features beyond standard semantic HTML.

What interviewers evaluate

  • Correctness of Infinite Scroll Logic: Does the feed correctly detect the scroll threshold and trigger subsequent data fetches?
  • State Management: Is the component's state (posts, loading, page number, hasMore) managed effectively and clearly?
  • Performance & Efficiency: Does the solution avoid unnecessary re-renders or API calls (e.g., debouncing/throttling scroll events, preventing multiple simultaneous fetches)?
  • Code Quality: Is the code well-structured, readable, and maintainable, adhering to React best practices and TypeScript types?
  • Handling Edge Cases: Does the solution gracefully handle the initial load, subsequent loads, and the "end of feed" scenario?

Loading editor...