T
tRPC

❓-help

Best Practice to Fetch a Query OnDemand

Mmichaelschufi5/25/2023
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) 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).
AKAlex / KATT 🐱5/25/2023
either you use enabled: on the query or you can use useContext https://trpc.io/docs/reactjs/useContext
Mmichaelschufi5/25/2023
useContext sounds nice, i'll try that! thanks 👍

Looking for more? Join the community!