How would you structure API calls in a large React app?

React data fetching and state libraries · Experienced

Model answer

Create a small API layer (one module with a shared fetch or axios instance for base URL, headers, error handling and token refresh), then wrap calls in data hooks such as useUsers or useCreateOrder built on TanStack Query. Components use hooks and never build URLs themselves.

This gives one place to change auth, logging and error format, and makes testing with mocks easier.

Learn this topic

TanStack Query (React Query): Fetching, Caching and Updating Server Data in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions