liveBook · Manning

React Query

As explained in React Hooks in Action: With Suspense and Concurrent Mode

React Query is a library for fetching, caching, synchronizing, and updating asynchronous server state in React applications.

React Query manages server state in React applications, reducing repeated loading indicators when views reuse the same data. It caches results, deduplicates requests, refreshes stale data in the background, and exposes status information for responsive interfaces. section 10.3 section 10.3.1

A typical setup proceeds through three focused steps:

  • Create a QueryClient and provide it around the application with QueryClientProvider. section 10.4
  • Fetch data with useQuery by supplying a query key, fetch function, and optional configuration such as initialData. section 10.3 section 10.4
  • Update server state with useMutation, then respond to completion through its mutation function and configuration. section 10.3 section 10.4

Shared bookables across application views

A bookings app loads bookables on both the Bookables and Bookings pages. Without caching, navigation reloads the same data and shows another loading indicator. React Query lets both views reuse cached data while background fetching obtains newer results; queryClient.getQueryData can also seed a query from an existing cache entry. section 10.3.1 section 10.4

const queryClient = new QueryClient();
 
export default function App () {
  return (
    <QueryClientProvider client={queryClient}>
      {/* app JSX */}
    </QueryClientProvider>
  );
}

Use the provider listing to see the minimal client setup that makes React Query available throughout the component tree. section 10.4

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Explains client setup, query keys, status, errors, background refetching, request deduplication, mutations, and cache access.
Examples
Demonstrates shared bookables and users, delayed json-server responses, query-cache reuse, mutations, and slow-loading user avatar images.
What it assumes
Assumes React and modern JavaScript familiarity while replacing a hand-built useFetch approach with managed server-state hooks.
What you can do after
Apply it via QueryClientProvider, useQuery, useMutation, query keys, configuration options, and queryClient cache methods.
How it connects
Connects React Query with React hooks, server state, query caches, mutations, React Router views, and Suspense-based loading experiences.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.