Attributes: ✅ Recommended • 🔧 Fixable
Overview
Query keys should be seen like a dependency array to your query function: Every variable that is used inside thequeryFn 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
- Independent Caching: Different values should create different cache entries
- Automatic Refetching: Queries refetch when dependencies change
- 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:Auto-Fix
This rule is auto-fixable. ESLint can automatically add missing dependencies to your query key:When to Disable
You might want to disable this rule if:- You’re using a custom query key factory that handles dependencies differently
- You’re intentionally sharing cache across different parameters (advanced use case)
- You’re using a global query function that doesn’t depend on variables
Best Practices
Use Query Factories
Use Query Factories
Create reusable query configurations with proper key structures:
Keep Keys Serializable
Keep Keys Serializable
Ensure all query key values are serializable:
Order Matters
Order Matters
Keep related queries together by using consistent key ordering:
TypeScript Support
The rule works seamlessly with TypeScript and understands type information:Related Rules
- no-unstable-deps - Prevents unstable values in query keys
- stable-query-client - Ensures stable QueryClient instances
Further Reading
Query Keys Guide
Learn best practices for structuring query keys
Effective Query Keys
TkDodo’s guide to query key patterns