LeoCaprilin
LeoCaprilin2y ago

A more clean way to know when a query is loading, or failed?

I know that when a query is undefined is loading but, could be better to have a boolean state of the query like react-query does? I don't know if there's a work around on this, or a custom implementation of something similar.
4 Replies
Web Dev Cody
Web Dev Cody2y ago
what type of error would you expect your query to throw? I'd personally just have the query return a custom error object structure and you can display in the UI based on that but from what I've seen, your convex query should be returning either an array of data, or an object | null if that thing was not found. I guess I'm not sure what you'd expect the query to error on. Like do you mean if just any type of exception was thrown?
ballingt
ballingt2y ago
We've seen folks write custom hooks like (psuedocode)
function useQueryObject(...) {
const data = useQuery(...);
if (data === undefined) {
return {loading: true, data};
} else {
return {loading: false, data}
}
}
function useQueryObject(...) {
const data = useQuery(...);
if (data === undefined) {
return {loading: true, data};
} else {
return {loading: false, data}
}
}
which, with the addition of some types, provides something closer to the react-query interface
Michal Srb
Michal Srb2y ago
Note that with Convex 1.3 https://news.convex.dev/announcing-convex-1-3/ you can now send data to the client with Errors. Similarly to Tom's example you could wrap useQuery to return such error data - that said usually expected errors happens more on mutations and actions, while queries can be structured as Web Dev Cody described.
Convex News
Announcing Convex 1.3
Convex 1.3 brings a new callback-based JavaScript client for using Convex in non-React apps and a way to propagate error information for better error handling UX. We’ve also made a bunch of improvements to our CLI and runtime! Callback-based JavaScript client The reactive paradigm is a great fit
LeoCaprilin
LeoCaprilinOP2y ago
Got it, thanks you guys! Really enyoing the DX that convex gives, feels smooth!

Did you find this page helpful?