useQuery returns
I noticed
useQuery
from the frontend doesn't return a promise. Does it automatically hold up the execution loop until the query is done?5 Replies
useQuery
is a react hook, so it is called while rendering react. the first time you call it for a query, it will return undefined
. then when the query finishes executing, it will trigger a React rerender during which it returns the query result. And whenever the query reruns (because the data changes) it will also rerender the UI.AHHH ok
I'm coming at this from an angle of writing Firebase functions to do server things, I've gotta wrap my brain around Convex zen lol
if you want to run a query a single time from an imperative environment (so it won't rerun when the data changes) you can use the
ConvexReactClient.query()
function which does return a promise https://docs.convex.dev/api/classes/react.ConvexReactClient#queryClass: ConvexReactClient | Convex Developer Hub
react.ConvexReactClient
If i
useEffect
on a useQuery
hook that should fire immediately on component load and then react to changes in the database right?Not sure but if this behaves at all like tanstack query, the correct way is probably to useQuery without useEffect and then handle the case that it returns undefined by conditionally rendering the component you want to use it for.