Skip to main content
Attributes: ✅ Recommended • 🔧 Fixable

Overview

The QueryClient contains the QueryCache, so you’d only want to create one instance of the QueryClient for the lifecycle of your application - not a new instance on every render. Creating a new QueryClient on every render will:
  1. Reset your entire cache
  2. Cause all queries to refetch
  3. Break optimistic updates
  4. Cause performance issues

Rule Details

This rule prevents creating a new QueryClient instance inside a component function body, which would cause it to be recreated on every render.

Examples

Incorrect Code

Correct Code

Exception: Server Components

It’s allowed to create a new QueryClient inside an async Server Component, because the async function is only called once on the server.
Server Component (Next.js App Router)
The rule understands async function contexts and won’t flag this pattern.

Common Patterns

App-Level Provider

The most common pattern is creating the QueryClient at the module level:
App.tsx

Component-Level Provider

Sometimes you need a provider lower in the tree. Use useState:
NestedProvider.tsx

Testing Setup

In tests, create a new client for each test:
test-utils.tsx

Why useState Over useMemo?

useState with a function initializer is the recommended approach because:
  1. Guaranteed single execution: The initializer function runs exactly once
  2. No dependencies: Unlike useMemo, you can’t accidentally provide wrong dependencies
  3. React concurrent mode safe: Works correctly with React 18+ concurrent features
  4. Clearer intent: Signals that this value should never change

Auto-Fix

This rule includes an auto-fixer that will wrap your QueryClient creation in useState:
Before:
After:
Review auto-fixes to ensure they’re appropriate for your use case.

Multiple QueryClients

Sometimes you need multiple QueryClient instances (e.g., for isolated contexts):

Framework-Specific Patterns

Next.js App Router

app/providers.tsx

Remix

app/root.tsx

Debugging

If you suspect your QueryClient is being recreated:

Further Reading

QueryClient

Learn about QueryClient configuration options

Important Defaults

Understand TanStack Query’s default behaviors