T
TanStack2y ago
like-gold

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
like-gold
like-goldOP2y 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()
absent-sapphire
absent-sapphire2y ago
onClick: queryClient.ensureQueryData.then…
like-gold
like-goldOP2y 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
correct-apricot
correct-apricot2y ago
useQuery is a hook because it ties into the react lifecyle and creates a subscription
like-gold
like-goldOP2y 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
correct-apricot
correct-apricot2y ago
That's what queryOptions is largely for

Did you find this page helpful?