TanStack Query
repository·main·Indexed 13 days ago
https://github.com/TanStack/queryA powerful async state management library that handles fetching, caching, and synchronizing server state across various web frameworks, including React, Angular, Preact, Lit, and React Native.
What's inside TanStack Query
- TanStack Preact Query provides hooks for fetching, caching, and updating asynchronous data within Preact applications. It is designed to be backend agnostic, meaning it works with any data fetching protocol such as REST, GraphQL, or standard Promises.
Overview of TanStack React Query
mainTanStack React Query provides React Hooks for fetching, caching, and updating asynchronous data. It is designed to be agnostic to your transport layer, meaning it works with REST, GraphQL, or any other protocol that returns promises. It manages complex state transitions like loading, error, and success states, and handles automatic caching and refetching logic.Overview of TanStack Query for Angular
mainThe
@tanstack/angular-query-experimentalpackage provides a first-class API for managing server state within Angular applications. It is designed to handle the complexities of fetching, caching, synchronizing, and updating asynchronous data.Important Note: This package is currently in an experimental stage. Breaking changes may occur in both minor and patch releases. If using this in production, it is recommended to lock your version to a specific patch-level version to avoid unexpected breaking changes.
Overview of TanStack Query
mainTanStack Query is an async state management library designed to simplify the process of fetching, caching, synchronizing, and updating server state. It is protocol-agnostic, meaning it can work with REST, GraphQL, promises, or any other fetching mechanism.
Key capabilities include:
- Caching & Synchronization: Automatic caching, refetching, and background updates.
- Data Fetching Patterns: Support for pagination, infinite scroll, and dependent queries.
- Mutations: Tools for updating server state.
- Advanced Features: Prefetching, request cancellation, and support for React Suspense.
Overview of TanStack Solid Query
mainTanStack Solid Query provides hooks for fetching, caching, and updating asynchronous data within Solid applications. It is a transport-agnostic library, meaning it works with any data fetching method such as REST, GraphQL, or standard Promises. It manages complex asynchronous states like caching, refetching, and synchronization automatically.Use Browser Extensions for TanStack Query Debugging
mainInstead of using framework-specific packages, you can use third-party browser extensions to debug TanStack Query directly in your browser's DevTools (Chrome, Firefox, or Edge). These provide equivalent functionality to the@tanstack/svelte-query-devtoolspackage.Browser Extensions for TanStack Query
mainFor Chrome, Firefox, and Edge users, third-party browser extensions are available to debug TanStack Query directly in the browser's native DevTools. These provide functionality similar to the framework-specific packages.What is MutationCache and when to use it
mainThe
MutationCacheis the storage for mutations. While it is the underlying storage, you will normally not interact with theMutationCachedirectly; instead, you should use theQueryClientfor most mutation operations.Use the
MutationCachedirectly when you need to implement global side effects that cannot be overridden by individual mutation options, or when you need to subscribe to the entire cache for state changes.import { MutationCache } from '@tanstack/react-query' const mutationCache = new MutationCache({ onError: (error) => { console.log(error) }, onSuccess: (data) => { console.log(data) }, })What is QueryCache and when to use it
mainThe
QueryCacheis the storage mechanism for TanStack Query. It stores all data, meta information, and the state of all queries it contains.Note: In most applications, you should not interact with the
QueryCachedirectly. Instead, use theQueryClientto manage your cache. Direct interaction is typically reserved for advanced scenarios, such as inspecting query state timestamps or subscribing to global cache updates.import { QueryCache } from '@tanstack/react-query' const queryCache = new QueryCache({ onError: (error) => { console.log(error) }, onSuccess: (data) => { console.log(data) }, onSettled: (data, error) => { console.log(data, error) }, })What is TanStack Query?
mainTanStack Query (formerly React Query) is a library designed to manage fetching, caching, synchronizing, and updating server state in web applications.
Unlike traditional state management libraries that focus on client state, TanStack Query is specifically built to handle the unique challenges of server state, which includes:
- Data that is persisted remotely and may be changed by other users.
- Data that requires asynchronous APIs to fetch or update.
- Data that can become "out of date" if not carefully synchronized.
It solves complex problems such as caching, request deduping, background updates, pagination, and memory management.
Limit infinite query memory usage with `maxPages`
mainTo prevent excessive memory consumption and slow refetching in infinite queries, TanStack Query v5 introduces the
maxPagesoption. This option limits the number of pages stored in the query data and subsequently refetched.To use this effectively for bi-directional infinite lists, ensure both
getNextPageParamandgetPreviousPageParamare defined.Handle SSR query errors and dehydration
mainBy default, Vue Query only includes successful queries in the dehydrated state. If a query fails on the server, it is excluded from dehydration, causing the client to see a loading state and attempt to refetch the query.
If you need to render an error page with a specific status code instead of showing a loading state, use
queryClient.fetchQueryand catch the error manually to handle the response.