Loading, error, data
Hello community. First time Convex user. How do I display a loading state (spinner) when using the
useQuery hook? I thought that this hook would return some kind of flag (like in react-query). But it seems to return only the final result. How do I handle the fetching status? (loading, error...) I could not find related documentation.7 Replies
Welcome! Convex's
useQuery uses the value undefined to mean loading.You mean as long as the response is "undefined", I should display a loading spinner?
Yep! You mention errors, errors are thrown from the hook (so error boundaries can be useful here)
Folks have created more traditional hooks that give
const {data, loading, error} = useQueryObject(), if you search the Discord you can see someWhat if I have a function, given a primary key returning 1 record (not array). But then if the record is not found, it returns undefine. How do I distinguish the "loading" undefined and the "result" undefined?
This
undefined will be converted to null; undefined is not one of the allowed Convex function return values.
so in React, undefined always means loading
while when using Convex from other languages we don't have to try to represent both JavaScript null and JavaScript undefined wiht e.g. only None in Python.Ok, cool. That works.
here's a bit more on the hook https://docs.convex.dev/client/react#fetching-data and valid convex types https://docs.convex.dev/database/types