T
TanStack7mo ago
rare-sapphire

beforeLoad vs. loader: Clearing Up ensureQueryData Usage

Sorry for the cryptic title, but I'm currently wondering what might be the best location for placing the ensureQueryData function in order to pre-load my data. Is this assumption correct? When I need the returned data in my RouteComponent (via Route.useLoaderData), I would place it in the loader, otherwise in beforeLoad.
4 Replies
eastern-cyan
eastern-cyan7mo ago
no, you can still use the data in the route component when returned from beforeload via route context all beforeLoads run serially vs loaders run in parallel beforeLoad runs upon every navigation vs. loader is potentially cached (not that relevant when using query) so there is not a definite answer when/where to call it but usually, you call it in the loader to avoid waterfalls
rare-sapphire
rare-sapphireOP7mo ago
Ah, that makes a lot of sense. Thanks a lot, @Manuel Schiller . So when I do not really need the data in the RouteComponent, I would simply return nothing from my loader.
eastern-cyan
eastern-cyan7mo ago
you wouldn't return it from the loader anyhow when using query usually you would still use useQuery etc in the component so just await it and then return nothing
rare-sapphire
rare-sapphireOP7mo ago
Perfect. Thanks a lot!

Did you find this page helpful?