T
TanStack•10mo ago
ambitious-aqua

Is it a bad practice to call `useQuery` in route components?

I wonder why we need loader in the first place. Can we just call useQuery and call it a day?
16 Replies
deep-jade
deep-jade•10mo ago
you can certainly to so however you won't benefit from preloading
ambitious-aqua
ambitious-aquaOP•10mo ago
So the benefit of using loader instead of useQuery is loading data before rendering route components?
deep-jade
deep-jade•10mo ago
not the only one but yes
ambitious-aqua
ambitious-aquaOP•10mo ago
Thanks, would you point me to docs where I can find other benefits of loader?
complex-teal
complex-teal•10mo ago
So that is really useful for preloading to have loader. Because when you hover over link the loader function is executed as I understand, right? Not only route js bundle is fetched but data it needs too.
ambitious-aqua
ambitious-aquaOP•10mo ago
Which means, I hover over a link to PageA then loader on PagaA route is called automatically?
complex-teal
complex-teal•10mo ago
Yes, when you have preload set to intent in your config. That seems petty nice stuff. Just adding preload intent and loaders to your routes and your website feels fast as hell
ambitious-aqua
ambitious-aquaOP•10mo ago
Good to know, thanks @Valerka ! Oh shoot, doc haywire is still ongoing. Asking for related docs is a bad idea.
deep-jade
deep-jade•10mo ago
not sure if we have docs about that explicitly . there are a few APIs that make more sense with loaderdata e.g. head()
ambitious-aqua
ambitious-aquaOP•10mo ago
I'm gonna look up for head() and tinker with it, thanks again! Default preloadStaleTime is 30secs but default staleTime is 0. So even I preloaded data for a route when I move to the route within 30 secs loader is called again. What's the meaning of this?
deep-jade
deep-jade•10mo ago
meaning is that you will be able to instantly show the data but the data might be stale so it fetches it again in the background and updates if it changed
ambitious-aqua
ambitious-aquaOP•10mo ago
Ahhh So when I move to the route, it shows preloaded data anyway and then run loader to fetch the latest data, shrewd!
deep-jade
deep-jade•10mo ago
yes
ambitious-aqua
ambitious-aquaOP•10mo ago
@Manuel Schiller Thanks to you, I have finally finished my TSR 101 tutorial.🎉
deep-jade
deep-jade•10mo ago
what does that mean? you created a tutorial?
ambitious-aqua
ambitious-aquaOP•10mo ago
Yes, it's in Japanese though. Now I can sell TSR hard here in Japan.

Did you find this page helpful?