KatInterview

Design and Implement a 2x3 React Post Grid with API Integration

System DesignModel answer
MediumModel answerSystem Designreactfrontendcss-grid

Last updated

Build a 2x3 React grid component for display feeds, integrate data loading with proper state management, and explain React hook mechanics.

In a live technical screen, you are asked to design and implement a React component that renders a social feed as a fixed $2 \times 3$ grid (2 rows, 3 columns of equal width). Each feed item includes the following properties:

FieldDescription
feed_idUnique string identifier
feed_textText content of the post
createdAtISO 8601 timestamp string representing post creation time

The problem must be solved in three stages:

Part 1: Static Grid Layout

Create a clean component hierarchy (e.g., Grid Container and Post Card) rendering hardcoded dummy data into a 6-cell layout ($2 \times 3$). Ensure that long post texts do not break the strict cell dimensions and equal column widths. Handle scenarios where fewer than 6 posts exist.

Part 2: API Integration & Async Logic

Replace the static data with dynamic fetching from a mock API endpoint (GET /api/v1/feed).

  • Display state logic: render loading spinners, network/HTTP error states, and empty states gracefully.
  • Data ordering & truncation: ensure exactly 6 items are displayed, ordered by createdAt descending (newest first).
  • Robustness: handle race conditions and prevent state updates on unmounted components using proper effect cleanups (e.g., AbortController).

Part 3: Deep Dive on React Hooks

Provide a comprehensive explanation of the React hooks utilized in your implementation (useState, useEffect, useCallback/useMemo, useRef), detail their lifecycles, discuss common pitfalls (such as stale closures), and demonstrate how to extract the fetching logic into a reusable custom hook (useFeedData).

Hints

Hint 1

For fixed 2x3 layouts, CSS Grid with `grid-template-columns: repeat(3, 1fr)` and fixed cell aspect ratio or `minmax` works best.

Hint 2

Remember to pass an `signal` from an `AbortController` into `fetch` inside `useEffect` and invoke `.abort()` in the cleanup function to prevent memory leaks and race conditions.

Hint 3

Ensure HTTP error status codes (e.g., 4xx, 5xx) are manually checked using `response.ok`, as `fetch` only rejects on network failures.