TanStack Query is the backbone of data fetching in modern React applications. Here are the patterns worth reaching for every time.
Query Options Factory
Always define query options outside components using the queryOptions helper. This gives you type-safe cache keys and makes invalidation predictable.
Optimistic Updates
For any user-facing mutation, update the cache immediately and roll back on error. The key is returning the previous state from onMutate so you have something to restore.
Mutation Scoping
When the same resource can be modified by multiple UI elements, mutation scope prevents race conditions by serializing mutations per resource ID.