Chapter 14: Fetching Data & APIs

Up until now, we’ve been using fake or static data. Today we’ll learn how to actually fetch data from APIs, handle loading spinners, show errors gracefully, and even get a taste of the modern way with TanStack Query (formerly React Query).

We’ll go very slowly and clearly, like I’m sitting next to you in Mumbai with our laptops open, chai on the table, and I’m explaining everything live with complete, copy-paste-ready examples.

1. Fetch API vs Axios – Which One to Use?

Feature Fetch API (built-in) Axios (library)
Included in browser Yes – no extra install No – need to install (npm install axios)
Promise-based Yes Yes
Automatic JSON parsing No – you must do response.json() Yes – automatic
Error handling Only rejects on network error (not on 404/500) Rejects on any non-2xx status too (easier)
Cancel requests Yes (with AbortController) Yes (with CancelToken)
Interceptors No Yes (great for auth tokens, logging)
Progress tracking Yes (via ReadableStream) Yes (onUploadProgress, onDownloadProgress)
Community & popularity Built-in – always available Extremely popular in React world

2026 Recommendation:

  • Small/simple projects or learning → use Fetch API (no extra dependencies)
  • Medium to large projects → use Axios (or better → TanStack Query which we’ll see soon)
  • Modern best practiceTanStack Query (handles caching, loading, errors, retries, background updates…)

2. Basic Fetching with Fetch API + Loading/Error States

Let’s build a real user list from JSONPlaceholder API.

Create src/components/UserList.tsx

tsx

Add to App.tsx:

tsx

3. Fetching with Axios (Cleaner Syntax)

Install Axios:

Bash

Same example with Axios:

tsx

Advantages:

  • response.data is already JSON
  • Better error handling (catches 404, 500 automatically)

4. Introduction to TanStack Query (React Query) – The Modern Way

TanStack Query is the best way to fetch data in 2026 React apps. It handles:

  • Loading states
  • Error states
  • Caching
  • Automatic retries
  • Background refetching
  • Pagination, infinite scroll, mutations…

Install:

Bash

Wrap your app in QueryClientProvider (in main.tsx):

tsx

Now create src/components/UserListQuery.tsx

tsx

Why TanStack Query wins:

  • No manual useState for loading/error/data
  • Automatic caching (data stays fresh)
  • Refetch on window focus, network reconnect
  • Mutations (POST/PUT/DELETE) are easy
  • Devtools for debugging

Summary – Chapter 14 Key Takeaways

  • Fetch API → built-in, good for learning
  • Axios → cleaner syntax, better errors
  • Always handle loading, error, success states
  • Use useEffect for basic fetching
  • TanStack Query = modern gold standard (caching, retries, background updates)
  • Never forget error boundaries in real apps

Mini Homework

  1. Create a /posts page that fetches posts from https://jsonplaceholder.typicode.com/posts
  2. Use TanStack Query + show loading skeleton
  3. Bonus: Click a post title → navigate to /posts/:id and fetch single post

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *