Skip to main content

Installation

Learn how to install and set up TanStack Query in your React application.

Requirements

TanStack Query is compatible with React v18 and v19.
  • React 18.0.0 or later
  • React 19.0.0 or later
React Query version 5.90.21 and above supports both React 18 and React 19. Make sure you’re using a compatible version.

Package Installation

1

Install the package

Install TanStack Query using your preferred package manager:
2

Install DevTools (Optional)

For development, install the DevTools package:
The DevTools package is tree-shakeable and only includes development code when NODE_ENV !== 'production', so it’s safe to include in production builds.

Setup

1

Create a QueryClient

Create a QueryClient instance to manage queries and mutations:
You can configure default options for all queries and mutations:
2

Wrap your app with QueryClientProvider

Provide the QueryClient to your React app using the QueryClientProvider:
3

Add DevTools (Development)

Import and add the DevTools component for development:
The DevTools component only renders in development mode (NODE_ENV === 'development'), so you don’t need to conditionally import it.

Complete Example

Here’s a complete setup example:
src/main.tsx

Configuration Options

Customize the QueryClient with these common options:

Query Options

Mutation Options

Be careful with aggressive retry settings in production, as they can impact server load and user experience.

Environment-Specific Configuration

Configure different settings for development and production:

Next.js Setup

For Next.js applications, create a separate client component:
app/providers.tsx
Then use it in your root layout:
app/layout.tsx
Always create the QueryClient inside a component using useState to ensure each request gets a new client instance in Server Components.

Persister Setup (Optional)

To persist query cache across sessions:

Verification

Verify your installation by creating a simple query:
src/App.tsx
If you see the DevTools floating button in the bottom-right corner of your app, React Query is successfully installed!

Next Steps

Quick Start

Build your first React Query application

TypeScript

Configure TypeScript for type-safe queries