Custom hooks are a common but flawed way to abstract TanStack Query usage. Wrapping useQuery in a custom hook breaks TypeScript type inference when passing options like UseQueryOptions without explicit generics, and limits usage to React component contexts only. The queryOptions API (introduced in TanStack Query v5) is a better abstraction: it's a plain function usable anywhere (hooks, server, route loaders, prefetching), preserves full type inference, and composes cleanly by spreading additional options at call sites rather than threading them through parameters.

7m read timeFrom tkdodo.eu
Post cover image
Table of contents
Custom HooksQuery OptionsTypeScript LibrariesFinding Better AbstractionsThe Query Options APIComposing QueryOptionsContinue Series
1 Comment

Sort: