useFetch looks simple but the interview is really about the race condition: if url changes while a request is in flight, a slow earlier response can land after the newer one and overwrite it.
active flag — set false in cleanup; never setState after the effect has been torn down (also silences the stale-response and unmount cases).AbortController — actually cancels the in-flight request on url change or unmount, instead of just ignoring it.setState; abort saves the wasted work. Use both.refetch that bumps a counter in the dependency array.Build it, then trigger the race: rapidly change url and confirm the final state matches the last request, not whichever resolved last.
import { useEffect, useState } from "react"; function useFetch(url) { // return { data, error, loading } // bonus: ignore stale responses and abort on url change / unmount } // Usage: // const { data, error, loading } = useFetch(`/api/user/${id}`);
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.