KatInterview

Build a Data-Fetching React Hook and Wire Up a Dashboard UI

Data Structure DesignModel answer
MediumModel answerData Structure DesignreacthooksuseEffect

Last updated

Write a React hook that fetches and reshapes API data with loading, error and refetch support, then connect a modal, a collapsible list and action buttons to component state.

You are working in a small React project that already has a stubbed API module and a basic page. Your job has two parts.

Part 1: a data hook

Write a custom hook for function components that:

  • Calls the provided API function inside useEffect, using an async function.
  • Converts each record from the API's shape into the shape the UI needs. For example, snake_case fields become camelCase, first and last name are merged into one display name, and a timestamp string becomes a Date.
  • Returns the transformed data together with loading and error state.
  • Exposes a way to refetch on demand.

Part 2: UI wiring

Change the existing page so that:

  • A modal can be opened and closed.
  • A list can be expanded to show every item and collapsed back to a short preview.
  • Several buttons each perform one simple, clearly defined action, such as refresh, toggle sort order, or open the details of an item.

Every interaction must be driven by component state and by the hook's output.

Because the starter code is not shown here, state the API and shape assumptions you make before you code. Explain how you handle slow responses, failures, empty data and unmounting.

Hints

Hint 1

The function passed to `useEffect` cannot be `async` because it must return either nothing or a cleanup function. Declare an async function inside the effect and call it.

Hint 2

Each effect run should own an `AbortController` and an `active` flag, both disabled in the cleanup. That way an outdated response can never overwrite newer state or update an unmounted component.

Hint 3

Implement refetch by bumping a counter that sits in the effect's dependency array, so refetching uses the same code path as the first load. In the UI, store as little state as you can (booleans, the selected item) and derive the visible list with `useMemo`.