Skip to main content

Quick Start

This guide walks you through building your first application with TanStack Query. We’ll use React in this example, but the concepts apply to all supported frameworks.

Prerequisites

Before you begin, make sure you have:
  • Node.js installed (version 16 or higher recommended)
  • A React application set up (using Vite, Create React App, or Next.js)
  • TanStack Query installed (see Installation)

Setup

1

Create a QueryClient

The QueryClient manages your queries and cache. Create one at the root of your application:
You can configure default options for all queries when creating the client:
2

Wrap your app with QueryClientProvider

The QueryClientProvider makes the QueryClient available to your entire component tree:
3

Create your first query

Use the useQuery hook to fetch and cache data. You need:
  • A unique queryKey to identify the query
  • A queryFn that returns a promise
TanStack Query automatically handles:
  • Caching the response
  • Deduplicating identical requests
  • Refetching when the window regains focus
  • Retrying failed requests
  • Updating loading and error states
4

Add DevTools (Optional)

The DevTools help you visualize and debug your queries during development:
The DevTools are automatically excluded from production builds, so you can safely leave them in your code.

Complete Example

Here’s the complete working example:

Understanding the Query Result

The useQuery hook returns an object with several useful properties:
  • data - The successfully fetched data (undefined until the query succeeds)
  • error - The error object if the query failed (null if successful)
  • isPending - true if the query is currently fetching for the first time
  • isError - true if the query encountered an error
  • isSuccess - true if the query was successful
  • isFetching - true whenever the query is fetching (including background refetches)
  • refetch - A function to manually trigger a refetch

Query Keys

Query keys uniquely identify your queries. They can be:
When any value in the query key changes, TanStack Query automatically refetches the data. This makes dependent queries trivial to implement.

What Happens Automatically?

With zero configuration, TanStack Query:
  1. Caches your data in memory
  2. Deduplicates identical requests that happen simultaneously
  3. Refetches data when the window regains focus
  4. Refetches data when the network reconnects
  5. Retries failed requests with exponential backoff
  6. Marks data as stale after a configurable time period
  7. Garbage collects unused cache data

Next Steps

Now that you’ve built your first query, explore more advanced features:

Query Options

Configure caching, refetching, and retry behavior

Mutations

Learn how to create, update, and delete data

Query Invalidation

Keep your data fresh after mutations

Pagination

Implement paginated and infinite scroll queries

Framework-Specific Examples

While this guide uses React, the concepts are identical across frameworks. The main differences are:
  • Vue: Use useQuery from @tanstack/vue-query with Vue’s Composition API
  • Solid: Use createQuery from @tanstack/solid-query as a primitive
  • Svelte: Use createQuery from @tanstack/svelte-query with Svelte’s reactive syntax
  • Angular: Use injectQuery from @tanstack/angular-query-experimental with Signals
Check the framework-specific documentation for detailed examples.