Best Practice to Fetch a Query OnDemand

What's the best practice to fetch a query on demand? I don't have the context for the query's input in the scope of my component. (Using react-hook-form with a zodResolver that has a dynamic refinement based on the fields submitted and a components prop)
3 Replies
Michael Schaufelberger
The thing is. The route is actually a query and not a mutation. But I need to access it in a react-query-mutation-way, because I need it on demand and not in the component lifecycle. Should I just change the procedure to be a mutation then? It feels wrong (E.g. returning undefined would be wrong).
Alex / KATT 🐱
either you use enabled: on the query or you can use useContext https://trpc.io/docs/reactjs/useContext
useContext | tRPC
useContext is a hook that gives you access to helpers that let you manage the cached data of the queries you execute via @trpc/react-query. These helpers are actually thin wrappers around @tanstack/react-query's queryClient methods. If you want more in-depth information about options and usage patterns for useContext helpers than what we provide...
Michael Schaufelberger
useContext sounds nice, i'll try that! thanks 👍