noob saibot
noob saibot2y ago

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
ballingt
ballingt2y ago
Welcome! Convex's useQuery uses the value undefined to mean loading.
noob saibot
noob saibotOP2y ago
You mean as long as the response is "undefined", I should display a loading spinner?
ballingt
ballingt2y ago
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 some
noob saibot
noob saibotOP2y ago
What 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?
ballingt
ballingt2y ago
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.
noob saibot
noob saibotOP2y ago
Ok, cool. That works.

Did you find this page helpful?