100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Web

React Query

A data-fetching and caching library for React (now TanStack Query)

IntermediateFramework4.1K learners

React Query (rebranded as TanStack Query) is a data-fetching and server-state management library for React that handles caching, background refetching, request deduplication, and stale-data invalidation for asynchronous data, removing the…

#ReactQuery#Web#Framework#Intermediate#SWR#React#RESTAPI#GraphQL#WebDevelopment#Glossary#SkillVeris

Definition

React Query (rebranded as TanStack Query) is a data-fetching and server-state management library for React that handles caching, background refetching, request deduplication, and stale-data invalidation for asynchronous data, removing the need to hand-roll this logic with useEffect and useState.

Overview

React Query was created by Tanner Linsley to address a gap that plain React state management (useState, useEffect, Redux, or Context) does not solve well: server state — data fetched from an API — behaves fundamentally differently from client state, because it can become stale, needs to be refetched, may be requested by multiple components simultaneously, and must be synchronized with a source of truth outside the application. React Query treats server-state management as its own concern, distinct from client-side UI state, and provides a dedicated caching and synchronization layer for it. At its core, the `useQuery` hook associates a unique 'query key' with an async fetch function; React Query caches the result keyed by that query key, automatically deduplicates identical in-flight requests from multiple components, and refetches data based on configurable rules — on window refocus, network reconnection, a stale-time interval, or manual invalidation. This eliminates large amounts of boilerplate that was previously hand-written per API call: loading/error/success state tracking, race-condition handling, and cache invalidation. For mutations (POST/PUT/DELETE-style operations), the `useMutation` hook provides similar lifecycle handling along with patterns for optimistic updates and automatic invalidation of related queries after a mutation succeeds, keeping the UI in sync with server state without manual refetch orchestration. The library also includes built-in support for pagination, infinite scrolling (`useInfiniteQuery`), and a devtools panel for inspecting the query cache during development. Rebranded as TanStack Query in 2021 to reflect framework-agnostic ports (Vue Query, Solid Query, Svelte Query), it remains overwhelmingly best known and most used in its original React form, and is now a near-default choice for server-state management in React applications, often used alongside — not instead of — client-state tools like Redux or Zustand.

Key Features

  • useQuery hook for declarative, cached, automatically refetched data fetching
  • Automatic request deduplication for identical in-flight queries
  • Configurable refetch triggers: window refocus, reconnect, stale-time intervals
  • useMutation hook with support for optimistic updates and cache invalidation
  • Built-in pagination and infinite-scroll support (useInfiniteQuery)
  • Framework-agnostic core with ports for Vue, Solid, and Svelte (as TanStack Query)
  • Devtools panel for inspecting and debugging the query cache
  • Eliminates most hand-rolled loading/error/success state boilerplate

Use Cases

Fetching and caching REST or GraphQL API data in React applications
Dashboards needing automatic background refresh of live data
Paginated and infinite-scroll data lists
Forms and admin panels using optimistic UI updates on mutation
Applications needing to deduplicate redundant API calls across components
Replacing Redux-based data-fetching boilerplate with a purpose-built server-state layer

Alternatives

SWR · VercelApollo Client · Apollo GraphQLRTK Query · Redux Toolkit team

Frequently Asked Questions

From the Blog