Feature request: async data from useQuery #5270
-
The idea is to provide Sometimes you need a data from the hook in the event handler, but at the moment when the event is fired, the data from the hook might be not be ready yet. There are few workarounds like disabling the handler before data is ready but this is not ideal in some cases. const { data, asyncData, isLoading } = useQuery()
async function handleClick() {
const data = await asyncData;
clickCallback(data.count)
}
return <div>{data.count}</div> It's possible to implement in the user land using a custom hook returning both |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
We are thinking about something like this, mainly to integrate better with suspense and the new One problem I see by sticking this into Note that you can always call |
Beta Was this translation helpful? Give feedback.
-
@TkDodo I mixed react router defer + react query Would like to if there is a better solution I share my code for others here:
|
Beta Was this translation helpful? Give feedback.
We are thinking about something like this, mainly to integrate better with suspense and the new
use
hook, but no decision have been made.One problem I see by sticking this into
useQuery
is that on first render, we don't have the promise yet, because it will be kicked off later, when the observer subscribes. This might even be delayed if we are currently restoring from a persister. So, where would we get thatasyncData
promise from?Note that you can always call
queryClient.fetchQuery()
in yourhandleClick
function and await it, and pass astaleTime
to it so that it only fetches if data isn't older than that staleTime. If you just want any data,queryClient.ensureQueryData()
also works.