Fetching Catalog Data
Introduction: The Need for Dynamic Data in a Catalog App
Welcome to the first lesson of our course, "Fetching Catalog Data". In this lesson, we will focus on how to fetch catalog data from an API and display it in our React app.
In a real-world catalog application, such as a library or bookstore, the list of items (like books) is always changing. New books are added, and old ones might be removed. Because of this, we need a way to fetch the latest data from our backend API and show it to users in real time.
In this lesson, you’ll fetch book catalog data from your API and render it efficiently. We’ll wire providers, define types, create an API helper, and then compare two approaches:
- TanStack Query’s
useQuery(recommended), and - a manual
useEffect+fetchimplementation (for contrast).
You’ll learn why useQuery scales better—caching, retries, de-duplication, background refetch, pagination support—while also seeing how to implement the same flow with plain hooks and what trade-offs you accept.
TanStack Query (formerly known as React Query) is a popular library that helps us fetch, cache, and update data in React applications. It makes handling data from APIs much easier and more reliable. In this lesson, you will learn how to use TanStack Query to fetch and display a list of books in your catalog.
Application Data Wiring: Providers, Types, and API Layer
We'll discuss the runtime “plumbing” our app uses to retrieve and display data: the Query Client provider, shared types, and an API module that calls /books. Each file is shown separately with an explanation.
Explanation
QueryClientis the app-wide cache/manager for queries.QueryClientProviderexposes TanStack Query to your entire tree (souseQueryworks anywhere below it).AuthProviderremains wrapped around your routed app to supply auth state.RouterProviderrenders pages that will now consume queries.
This placement ensures every page and component can leverage caching, retries, and background refetching provided by TanStack Query.
Explanation
Bookmodels the server representation your UI expects.PaginatedBooksResponsematches the API’s paginated envelope for/books(list + counts). Typed responses make your UI and data transforms safer.
Explanation
-
The backend responds with an envelope
{ success, data, ... }. -
We unwrap the
dataso components consume a typedPaginatedBooksResponsedirectly. -
Centralizing this in
api/books.tsavoids duplicating response-shape knowledge in your components. -
The
index.tsxfile sets up our React app, including the router and TanStack Query provider. -
The
Bookinterface defines the shape of a book object. -
The
getBooksfunction is used to fetch a list of books from our API.
This setup ensures our app is ready to fetch and display data.
