I was told a few months ago by @charred wing that a good pattern to use tanstack-query with tanstack-router was:
export const Route = createFileRoute('/')({
component: RouteComponent,
// ---
beforeLoad: ({ context: { trpc }, search }) => ({
queryOptions: trpc.route.handler.queryOptions(search)
}),
loader: ({ context: { queryClient, queryOptions } }) =>
queryClient.prefetchQuery(queryOptions)
})
function RouteComponent() {
const { queryOptions } = Route.useRouteContext()
const { data } = useSuspenseQuery(queryOptions)
}
which indeed, is an ok pattern. a bit verbose imho but does cover preloading from the router while maintaining access to subscriptions, etc etc.
now... this does not play well if you need to make cascading queries ; so how would i handle to write queryOptions once with cascading queries?