Module 38: API Integration

Lesson 2 of 5 · 15–25 minutes

Fetching with loading and error states

🏠The everyday picture

A good waiter says "your food is being prepared", or "sorry, we're out of that, may I suggest…". They never just disappear, leaving you staring at an empty table.

📘What it really is

npm install @tanstack/react-query
const queryClient = new QueryClient();
<QueryClientProvider client={queryClient}><App /></QueryClientProvider>
const { data, isPending, isError, error, refetch } = useQuery({
  queryKey: ["enquiries"],
  queryFn: getEnquiries,
});

if (isPending) return <p role="status">Loading enquiries…</p>;
if (isError) return <ErrorMessage message={error.message} onRetry={refetch} />;
return <EnquiryList enquiries={data} />;

🖱️Try it

Install TanStack Query. Replace your reducer's starting data with useQuery for the dashboard and for the detail page (["enquiry", id]). Stop json-server and reload, to see your error state, then start it again and press Try again. In developer tools, set the network to "Slow 4G" to see the loading state.

✅Quick check

What three states should every screen that fetches data handle?

Show the answer

Loading, error (with a way to retry) and success (including empty).

Two components both use useQuery({ queryKey: ["enquiries"] ... }). How many requests are made?

Show the answer

One. They share the same cached data.

← PreviousAn API client and a practice server
ProLearners Assist