Some way to check if the query in Next.js is loading or some way to check if the query was skipped?
I currently have a problem where I want to check if my query was skipped or if it is just waiting for the response.
27 Replies
You can:
1. check the dashboard for logs to see if the function ran
2. console log the result of your conditional there
So I basically have multiple options:
- I'm waiting for the response: Just wait longer if the response shows that no value is there and in the global state is no city than just redirect the person to /search
- In the url is no city but in the global state is in the city: just put the city in the url and start again
- If the value is only in the url it's fine we can just use that
Yes, but I cant programmatically see if the function runs right? I want to have something like a loading state or something to see if the function runs.
As long as
useQuery()
is returning undefined, it's loading - anything else, it's no longer loading. If you want both loading and skipped to appear as loading, you can combine those two conditions.
Oh nice, I will try it out
This seems not to work...
if there is no query.cityId (it's undefined) and because of that the convex query returns undefined isLoading is true even if it isn't loading
Oh if you only want isLoading to be true if query.cityId is defined, just switch the logic for that part
That will only evaluate true if
query.cityId
is defined and useQuery()
is returning undefined. In this case you know the query is not being skipped, is so it's actually loading.@erquhart Ok, but unfortunately the is loading thing turn into false before there is an actual value.
As you can see here the cityByIdIsLoading is first getting false and after that the actual value is there

so it's like:
not loading and no value <- Here I think is the problem because in this state my app thinks that there is no data
loading and no value
not loading and value
Right, if query.cityId is not a string you're effectively "pre-loading"
So if you want to do something different before loading actually starts, you can check for that first
Interesting... mhmm... idk man how to do that. but maybe for the convex part it's done and I have to figure out why Next.js does not give me the value immediately
seems weird that my url params are first not there and then they come all of the sudden
could you tell me more about pre-loading? so how can I find out if it's the actual url param that is undefined or the url param is not loaded yet
Sorry, by "preloading" I just mean loading hasn't actually started since you don't have the query param yet
Is it taking like a few seconds for next to provide the query params or is it really short?
idk, why does this matter? should I make a timeout or something like that?
If it's short you could just treat both states as loading, unless this is a view where the query string can keep changing and you need to keep loading to update
Ah just reread your original post, you're navigating based on this state so you need resolution before moving forward
Yeah delayed query params is a problem. Is this a client or server component?
This app is still on the pages dir so it's a client component
maybe I need to switch in the future (i get more and more problems with it)
Ah!
useRouter()
returns an isReady
prop
actually...
that may be oldoh
but they should have something to that effect
it is safe to use?
I think it works now. with the isReady prop, update you if it isn't working anymore
Looks like they recommend using
useSearchParams
from next/navigation
instead: https://nextjs.org/docs/app/api-reference/functions/use-router#router-eventsFunctions: useRouter | Next.js
API reference for the useRouter hook.
instead of query
oh isReady is there?
useSearchParams only exist on app dir 🫤
interesting, it's not in the docs
ah, gotcha

if isReady is working, great! undocumented though it seems, so may want to keep an eye on it. Are you using an old version of next maybe?
ooohhh
Functions: useRouter | Next.js
Learn more about the API of the Next.js Router, and access the router instance in your page with the useRouter hook.
I guess App Router is different
yes, they changed the apis for app router