How to use TRPC query without hooks
Hi guys! I'm new to TRPC so I see how you can fetch stuff in the backend with trpc using hooks like so
const { data: videos } = api.videos.fetchVideos.useQuery();
but in my case I want to fetch once an action happens and pass in an argument that I get once user submits something. What is the best way to go about this?Solution:Jump to solution
useMutation() | tRPC
The hooks provided by @trpc/react-query are a thin wrapper around @tanstack/react-query. For in-depth information about options and usage patterns, refer to their docs on mutations.
7 Replies
not sure if this is best way, but you can set enable to false in the query props, and destruct refetch, and call refetch whenever you want
use a mutation.
If the fetch is triggered by some other action, i.e user input, you want to use a mutation.
Solution
useMutation() | tRPC
The hooks provided by @trpc/react-query are a thin wrapper around @tanstack/react-query. For in-depth information about options and usage patterns, refer to their docs on mutations.
gotcha so the usemutation shouldn't be used for post type requests?
Just for any request where you need to attach arguments once an event happens?
More less yeah
Think about the difference being "do I want to automatically call x, or manually call x" for choosing query vs mutation respectively
^ this is bascially my logic behind selecting what one to use
mmm gotcha interesanchi
I appreciate you fellas