React Query
A data-fetching and caching library for React (now TanStack Query)
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…
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
Alternatives
Frequently Asked Questions
From the Blog
Build a To-Do List App in React
A comprehensive guide to build a to-do list app in react — written for learners at every level.
Read More Projects & Case StudiesProject: Build a Full-Stack To-Do App with React, Node.js and MongoDB
A full-stack to-do app is the perfect first MERN project — it covers every concept you'll use in production: REST APIs, database CRUD operations, JWT authentication, and deploying a frontend and backend separately. Build it once, understand the full stack.
Read More ProgrammingReact Hooks Explained: useState, useEffect, and Beyond
React Hooks replaced class components and changed how React developers think about state and side effects. This guide explains useState, useEffect, useContext, useRef, and custom hooks clearly, with practical examples for each.
Read More Learn Through HobbiesLearn React Through Game Development: Build a Chess Clock
A chess clock is the perfect React project — it's small enough to finish in an afternoon but teaches useState, useEffect, timer management, and conditional rendering in a context that makes every concept feel purposeful. No boring counter apps.
Read More