react-query is a robust and widely adopted library for managing, caching, and synchronizing asynchronous data within React applications. This registry entry refers to version 3.x.x of the `react-query` package, with 3.39.3 being the latest patch provided. Since late 2021, active development and new major versions (v4, v5) have transitioned to the `@tanstack/react-query` package as part of a broader monorepo rebrand. Consequently, the `react-query` package (v3) is now in a maintenance-only phase, receiving critical bug fixes but no new features. Its key differentiators include declarative data fetching via hooks (`useQuery`, `useMutation`), automatic background refetching, efficient data caching with configurable stale-time and cache-time, optimistic updates, and a powerful devtools extension. It significantly reduces boilerplate for data management compared to traditional state management solutions for remote data.
npm install react-queryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up QueryClientProvider and fetching data with useQuery to display a list of todos. It includes basic loading and error states.
Migrate your application to `@tanstack/react-query` by updating package names and reviewing breaking changes in the TanStack Query migration guides (e.g., `isLoading` vs `isInitialLoading`, query function return types). A codemod is available for some migrations.
Ensure you are instantiating and providing a `QueryClient` via `QueryClientProvider` at the root of your application. Directly manipulating `QueryCache` may require changes to use `QueryClient` methods.
Wrap `fetch` calls with logic to `throw new Error()` if `response.ok` is false. For Axios, errors are typically thrown automatically.
Review `useQuery` calls, especially with the `enabled` option, and update `isLoading` checks to `isInitialLoading` if the query is disabled. Change query functions returning `undefined` to return `null` or throw an error.
Wrap your application or the relevant component tree with `QueryClientProvider`, passing an instance of `QueryClient` as the `client` prop. Place it high in the component hierarchy, typically in `App.js` or `index.js`.
Ensure `useQuery` is called directly within a React functional component or a custom hook that itself adheres to the rules of hooks.
Narrow the type of the `error` object before accessing its properties, typically using `if (error instanceof Error)` or a type guard for specific error types like `AxiosError`.