Stan aplikacji

state/query-key-deps

A TanStack Query key holds every variable its query function reads, so a new id fetches new data instead of showing the old.

poważnastatycznefe-data-fetching

Dlaczego

The key is the cache address and the only thing TanStack Query watches. With queryKey: ['todo'] and queryFn: () => getTodo(id), every todo shares one cache entry: open todo 2 after todo 1 and the page shows todo 1's data, and changing id never refetches.

Poprawka

Put every value the query function uses into the key, in order from general to specific: queryKey: ['todo', id], ['todos', { status, page }]. In Vue pass the ref or getter itself (['todo', id]), not id.value. Build keys and functions together with queryOptions() or a key factory so they cannot drift apart, and enable @tanstack/query/exhaustive-deps in ESLint.

Przykłady

// bad
useQuery({ queryKey: ['todo'], queryFn: () => getTodo(todoId) })
// good
useQuery({ queryKey: ['todo', todoId], queryFn: () => getTodo(todoId) })

Źródła

Fakty

Identyfikator
state/query-key-deps
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx vue svelte
Tylko w projektach z
tanstack-query
Uczy jej skill
fe-data-fetching Data fetching
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain state/query-key-deps