query key
The array that names a piece of cached server data in TanStack Query, such as ["users", "detail", "10"].
In more detail
Keys are hierarchical, so invalidating ["posts"] also invalidates every key that starts with it. A key factory is a small object of functions that builds all keys for one resource, so no one hand-types a string and gets it wrong.
Example
detail: (id: string) => [...keys.all, "detail", id] as constRelated terms
- TanStack Query — A library that fetches, caches and refreshes server data in frontend apps, and runs changes to it as mutations.
- cache invalidation — Marking cached copies as out of date, or deleting them, when the real data changes so the next read fetches fresh data.
- as const — The TypeScript suffix as const, which tells the compiler to treat an object's values as fixed literal values rather than general types.
Taught in
- Type-Safe Client Generation: Orval & Automated SDKs — Production Full-Stack Engineering
- Optimistic UI Architecture: Cache & Rollback Protocols — Production Full-Stack Engineering