Skip to main content
Attributes: ✅ Recommended • 🔧 Fixable

Overview

Query keys should be seen like a dependency array to your query function: Every variable that is used inside the queryFn should be added to the query key. This makes sure that queries are cached independently and that queries are refetched automatically when the variables change.

Rule Details

This rule enforces that all variables referenced in your query function are included in the query key. This is crucial for proper caching and automatic refetching behavior.

Why This Matters

  1. Independent Caching: Different values should create different cache entries
  2. Automatic Refetching: Queries refetch when dependencies change
  3. Stale Data Prevention: Ensures you’re always working with the correct data

Examples

Incorrect Code

Correct Code

Common Patterns

User-Specific Queries

Filtered Lists

Paginated Data

Detail Views

What About Constants?

Constants that never change don’t need to be in the query key:
However, configuration that might change should be included:

Auto-Fix

This rule is auto-fixable. ESLint can automatically add missing dependencies to your query key:
Review auto-fixes carefully. The rule may not always detect the optimal query key structure.

When to Disable

You might want to disable this rule if:
  1. You’re using a custom query key factory that handles dependencies differently
  2. You’re intentionally sharing cache across different parameters (advanced use case)
  3. You’re using a global query function that doesn’t depend on variables
Disabling this rule can lead to stale data and caching bugs. Only do so if you fully understand the implications.

Best Practices

Create reusable query configurations with proper key structures:
Ensure all query key values are serializable:
Keep related queries together by using consistent key ordering:

TypeScript Support

The rule works seamlessly with TypeScript and understands type information:

Further Reading

Query Keys Guide

Learn best practices for structuring query keys

Effective Query Keys

TkDodo’s guide to query key patterns