React hook for API data and UI changes in a starter app
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Scale AI's coding round, last asked Sep 2025. It tests react, frontend and hooks.
You are given a starter React app with an API client and some UI already written. First write a custom hook that calls the API and converts the response into the shape the UI needs. Then change the UI as asked: open and close a modal, expand and collapse a list, and add a few buttons that perform simple actions, all wired to component state and to your hook. Use function components, hooks, and useEffect with an async function.
Part 1: the hook
Unless you agree on a different interface with the interviewer, assume:
- The provided
fetchUsers(): Promise<ApiUser[]>returns{ id, first_name, last_name, team_id, active }objects. -
useUsers()returns{ data, loading, error, refetch }, wheredatais{ [teamId]: { id, name, active }[] }: users grouped by team,namejoined from first and last name.
Part 2: the UI
- A "Details" button on each user opens a modal with that user's data; a close button and Escape close it.
- Each team's list expands and collapses.
- Buttons: "Refresh" calls
refetch; "Show active only" toggles a filter.
Example
fetchUsers() -> [{id: 1, first_name: "Ada", last_name: "Li", team_id: "t1", active: true},
{id: 2, first_name: "Bo", last_name: "Ng", team_id: "t1", active: false}]
useUsers().data -> { t1: [{id: 1, name: "Ada Li", active: true},
{id: 2, name: "Bo Ng", active: false}] }
"Show active only" on -> team t1 lists only Ada Li
Clarifying questions
- What exactly is the target shape of the data?
- Should
refetchshow the old data while loading, or clear it? - Should the expanded or collapsed state survive a refetch?
- Is there a design for the modal, and must it be accessible?
What a strong answer covers
-
useEffectcannot take an async function directly: define one inside and call it; handle loading and error states. - Ignoring a response that arrives after unmount or after a newer request (a cancelled flag or
AbortController), so no state is set on an unmounted component. - The transform kept pure and memoized, separate from fetching.
- UI state in the right place: which modal is open and which lists are expanded in component state; the filter derived, not stored as a second copy of the data.
- An accessible modal (focus moves in and back, Escape closes it,
aria-modal), buttons with labels. - Tests for the hook with a mocked API, including the error path.
Follow-ups
- Cache the response so two components using
useUsersmake one request. - Add optimistic toggling of a user's
activeflag through the API, with rollback on failure. - The list has 5,000 users. What changes?
Where it's asked
What it tests
react · frontend · hooks · async
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
