Corbi-Wan KenobiC
Convex Community12mo ago
24 replies
Corbi-Wan Kenobi

When to query data in a framework's loader vs a route component? Or imported component?

Coming from a Remix/RR7 background where basically all data fetching was done in the loader. Now I'm making headway in TanStack Start/Router and Convex and its slowly starting to click. Convex docs for TanStack always show doing a query in the route component, usually highlighting the reactivity for changes in the database which is great.

My questions are, under what circumstances would one do the query in a loader/beforeLoad and when would one do it in the route component? When reactivity is needed is it only achieved in the route component? Can reactivity happen from an imported component that has a useQuery in its export? If one doesn't need reactivity is it better to query in the loader?

Some of this has to do with the code splitting that TanStack does, keeping code securely processed on the server vs potentially exposed on the client. How that all works is one of the many ways my ignorance starts to show. Like anyone else, I want to keep safe the data that needs keeping safe, and also have the app do the work where it should be doing it (loader/component/route) and not mistakenly trying to make it do it one way when it only works the other.
Was this page helpful?