configure ConvexReactClient to skip queries when input is undefined by default?
Hi team! Is it possible to enable "skip" when doing
useQuery
in the client? currently im adding a "isLoading" everywhere when i do useQuery
.
i've checked the docs and dont see anything related. thanks!9 Replies
Hey @winsoroaks, your code looks good! What's making customerId undefined? Are you fetching it from another query? For now we suggest grouping fetching into a single query where possible / ergonomical, which avoids the waterfall (waiting for customerId before fetching the customer).
Alternatively to "skip" you could pass down undefined customerId to the server and handle it inside
getCustomer
, but this does incur an additional functional call (which might not be a problem in practice).ok got it! thank you 🙂 that was an example. but most of the time, i think i've been relying on
userId from clerk to do a query
You could move authentication at a higher level in your app, so no other query is mounted until you are done with user authentication.
If that's not possible you could use a helper hook like this:
Also if you follow our Auth integration, you should have access to the user ID via
ctx.auth.getUserIdentity()
without relying on passing it as an argument to a query (which might also not be secure depending on your setup)great idea, thanks!
cool, i'll give this a shot. i anecdotally ran into undefined a few times while the doc says, "getUserIdentity is guaranteed to have tokenIdentifier, subject and issuer fields." maybe i forgot to add a bang to the identity. will try and see how it goes. thanks, once again 🙂
tried it out, even adding a bang didnt quite work out.
doing the following works
Ah, so you do have a working auth setup, but you don't want to run the
useQuery
before the client is logged in. That makes sense, and you can avoid the "skip" if you only render the component when the client is authenticated:
https://docs.convex.dev/auth/clerk#logged-in-and-logged-out-viewsConvex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
The only problem with this in hooks is that it's not reactive, so your component would not re-render when the authentication state changes.
@Aps everything should be reactive, what do you see is not updating for you?
I mean if you use directly
ctx.auth.getUserIdentity()
in your client side, you will not receive an event when identify is changed.