T
TanStack2y ago
ambitious-aqua

Getting a promise from useQuery

I have a button which navigates to some screen when clicked, but this navigation requires some initial data which has defaults stored in user settings. I am fetching user settings with useQuery. Instead of disabling my button until the settings have been loaded, I would rather be able to get a promise from useQuery somehow so that I can await this on-click (given that settings haven't loaded by the time the user clicks). I know I can just refetch, but that would be a bit slower. Is there any way to do this natively? Thanks
6 Replies
ambitious-aqua
ambitious-aquaOP2y ago
Creating a custom hook for this seems easy enough but I would rather have an opinionated approach Would be nice to have a useQuery().await() or something that returns the same shape as refetch()
xenial-black
xenial-black2y ago
onClick: queryClient.ensureQueryData.then…
ambitious-aqua
ambitious-aquaOP2y ago
Ahh perfect, thanks. Why isnt it available on useQuery? 🤔 Actually why is useQuery even a hook to create queries? Would make more sense to define the queryfn in kne place rather than every useQuery invoxation So const useQueryX = createQuery makes more sense to me
deep-jade
deep-jade2y ago
useQuery is a hook because it ties into the react lifecyle and creates a subscription
ambitious-aqua
ambitious-aquaOP2y ago
Yeah i understand, but i would think a hook factory would make more sense as now the query fn is defined in every useQuery call rather than only once in the argument provided to the factory
deep-jade
deep-jade2y ago
That's what queryOptions is largely for

Did you find this page helpful?