T
TanStack9mo ago
solid-orange

What are some common practices regarding queryOption abstractions?

I basically have two hooks for each API request now. One is strictly queryOptions that utilize the queryKey and queryFn that allows me to spread into any useQuery hook. For most of my API handlers i abstract it even more by doing something like useResource(). These call useQuery and spread in the queryOptions. The most annoying part is handling types. The queryFn itself validates props (so it can correctly return an Error that RQ can consume), and so props coming into every other hook must be partial. This however does not give confidence to the developer what is actually needed and what is optional. So I’m curious how other people use abstraction with RQ
2 Replies
rare-sapphire
rare-sapphire9mo ago
Query Options | TanStack Query React Docs
One of the best ways to share queryKey and queryFn between multiple places, yet keep them co-located to one another, is to use the queryOptions helper. At runtime, this helper just returns whatever yo...
xenial-black
xenial-black9mo ago
The Query Options API
v5 brought a new, powerful API, especially if you're using React Query with TypeScript...

Did you find this page helpful?