Overview

React Query, a component of TanStack Query, provides a set of hooks for fetching, caching, and updating asynchronous data in React applications. It addresses challenges associated with managing server state, which differs from client state in characteristics such as persistence, shared access, and the potential for out-of-sync data. React Query aims to simplify these complexities by offering tools to maintain a consistent user interface while interacting with external APIs.

The library automates many common data fetching concerns. This includes caching fetched data to prevent redundant requests and improve perceived performance, background revalidation to ensure data freshness, and de-duplication of requests to optimize network usage. Developers can define queries using simple hooks, and React Query handles the underlying logic for data lifecycle management, error handling, and loading states.

React Query is suitable for applications that heavily rely on external data sources, such as e-commerce platforms, dashboards, and content management systems. Its capabilities extend to managing complex UI patterns like infinite scrolling and pagination, where data needs to be loaded incrementally. The library's approach to optimistic UI updates allows applications to respond immediately to user actions, providing a smoother experience while actual data mutations are processed on the server. For example, when a user marks an item as complete, the UI can update instantly, and React Query will synchronize the change with the backend, rolling back if the server operation fails.

While client-side state management libraries like Redux manage application-specific data, React Query focuses specifically on server state. This distinction is important because server state typically has different requirements for caching, invalidation, and synchronization across multiple clients. The official React Query documentation provides a detailed overview of its core concepts and usage patterns for managing server state effectively in a React environment.

React Query also integrates with modern web development practices, including server-side rendering (SSR) and static site generation (SSG), by providing mechanisms to prefetch data and hydrate the client-side cache. This enables faster initial page loads and improved SEO. The library's modular design allows developers to adopt it incrementally into existing projects or use it as a foundational piece in new applications.

Key features

  • Automatic Caching: Caches query results in memory to avoid redundant network requests and improve application responsiveness. This caching mechanism can be configured with various stale-time and cache-time options.
  • Background Data Revalidation: Automatically re-fetches data in the background when certain conditions are met, such as component mounts, window refocuses, or network reconnections, ensuring data freshness.
  • Query De-duplication: Prevents multiple identical data fetches from occurring simultaneously, consolidating requests to optimize network usage and server load.
  • Optimistic UI Updates: Allows immediate UI updates in response to user actions, providing a snappier user experience, while React Query handles the underlying data mutation and potential rollback on error.
  • Pagination and Infinite Scrolling: Provides utilities and patterns to efficiently load and manage large datasets in paginated or infinitely scrolling lists, reducing initial load times.
  • Error and Loading State Management: Exposes clear states for queries (isLoading, isError, isSuccess) and mutations, simplifying error handling and UI feedback mechanisms.
  • Data Synchronization: Ensures that all instances of a specific query across different components are updated when new data is fetched or mutated, maintaining data consistency throughout the application.
  • TypeScript Support: Built with TypeScript, offering strong type safety and improved developer experience with type inference for queries and mutations.

Pricing

As of May 7, 2026, React Query is distributed under an open-source license and is available for use without charge. The project is maintained by the TanStack community.

Feature Availability Notes
Core Library Free All core React Query features are open-source.
Commercial Support Not offered directly Community support available via GitHub and Discord.

Common integrations

  • React: React Query is specifically designed for integration with React applications, leveraging React hooks for its API (React Query overview).
  • Axios/Fetch API: Commonly used with standard HTTP clients like Axios or the native Fetch API for making actual network requests to RESTful or GraphQL endpoints.
  • Next.js: Integrates with Next.js for server-side rendering (SSR) and static site generation (SSG) to prefetch and hydrate data on the server (React Query SSR guide).
  • React Native: Can be used in React Native applications to manage server state in mobile environments (React Native getting started).
  • GraphQL Clients: While not a GraphQL client itself, React Query can wrap data fetching logic from GraphQL libraries like Apollo Client or Relay to manage caching and synchronization.
  • Zustand/Jotai: Can be used alongside client-side state management libraries for handling local UI state, while React Query specializes in server state.

Alternatives

  • SWR: A React Hooks library for data fetching that focuses on revalidation strategies, using a "stale-while-revalidate" approach.
  • Apollo Client: A comprehensive state management library for GraphQL, offering sophisticated caching, normalized caching, and GraphQL-specific features.
  • RTK Query: Part of Redux Toolkit, providing a powerful data fetching and caching solution built on Redux, with automatic client-side caching and API code generation.

Getting started

To begin using React Query in a React application, you first need to install it and set up a QueryClientProvider. This provider will make the query client available to all components within your application tree.

Here's a basic example demonstrating how to fetch data using the useQuery hook:

import React from 'react';
import ReactDOM from 'react-dom/client';
import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query';

// Create a client
const queryClient = new QueryClient();

function App() {
  const fetchTodos = async () => {
    const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
    if (!res.ok) {
      throw new Error('Network response was not ok');
    }
    return res.json();
  };

  const { isLoading, error, data } = useQuery({ queryKey: ['todo'], queryFn: fetchTodos });

  if (isLoading) return "Loading...";
  if (error) return "An error has occurred: " + error.message;

  return (
    <div>
      <h1>{data.title}</h1>
      <p>Status: {data.completed ? 'Completed' : 'Pending'}</p>
    </div>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
  <React.StrictMode>
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  </React.StrictMode>
);

This example demonstrates the basic steps:

  1. Install React Query: npm install @tanstack/react-query or yarn add @tanstack/react-query.
  2. Create a QueryClient instance: This client manages the cache and query lifecycle.
  3. Wrap your application with QueryClientProvider: This makes the query client accessible to all child components.
  4. Use the useQuery hook: Pass a unique queryKey and an asynchronous queryFn to fetch data.
  5. Handle loading and error states: The useQuery hook returns `isLoading`, isError, and data properties to conditionally render UI based on the query status.

For more advanced usage, including mutations, invalidation, and optimistic updates, refer to the official React Query API documentation.