useContext
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 here, we will link to their respective @tanstack/react-query docs so you can refer to them accordingly.
In v10, useContext no longer exposes the raw queryClient. If you need some of the methods that aren't wrapped by tRPC yet, import and use them directly from @tanstack/react-query:
tsx
tsx
Usage
useContext returns an object with all the available queries you have in your routers. You use it the same way as your trpc client object. Once you reach a query, you'll have access to the query helpers. For example, let's say you have a post router with an all query:
server.tstsinitTRPC } from '@trpc/server';import {z } from 'zod';constt =initTRPC .create ();constappRouter =t .router ({post :t .router ({all :t .procedure .query (() => {return {posts : [{id : 1,title : 'everlong' },{id : 2,title : 'After Dark' },],};}),}),});export typeAppRouter = typeofappRouter ;
server.tstsinitTRPC } from '@trpc/server';import {z } from 'zod';constt =initTRPC .create ();constappRouter =t .router ({post :t .router ({all :t .procedure .query (() => {return {posts : [{id : 1,title : 'everlong' },{id : 2,title : 'After Dark' },],};}),}),});export typeAppRouter = typeofappRouter ;
Now in our component, when we navigate the object useContext gives us and reach the post.all query, we'll get access to our query helpers!
MyComponent.tsxtsxMyComponent () {constutils =trpc .useContext ();utils .post .all .f ;// [...]}
MyComponent.tsxtsxMyComponent () {constutils =trpc .useContext ();utils .post .all .f ;// [...]}
Helpers
These are the helpers you'll get access to via useContext. The table below will help you know which tRPC helper wraps which @tanstack/react-query helper method. Each react-query method will link to its respective docs/guide:
| tRPC helper wrapper | @tanstack/react-queryhelper method | 
|---|---|
| fetch | queryClient.fetchQuery | 
| prefetch | queryClient.prefetchQuery | 
| fetchInfinite | queryClient.fetchInfiniteQuery | 
| prefetchInfinite | queryClient.prefetchInfiniteQuery | 
| ensureData | queryClient.ensureData | 
| invalidate | queryClient.invalidateQueries | 
| refetch | queryClient.refetchQueries | 
| cancel | queryClient.cancelQuery | 
| setData | queryClient.setQueryData | 
| getData | queryClient.getQueryData | 
| setInfiniteData | queryClient.setInfiniteQueryData | 
| getInfiniteData | queryClient.getInfiniteData | 
❓ The function I want isn't here!
@tanstack/react-query has a lot of functions that we haven't put in the tRPC context yet. If you need a function that isn't here, feel free to open a feature request requesting it.
In the meantime, you can import and use the function directly from @tanstack/react-query. We also provide a getQueryKey which you can use to get the correct queryKey on the filters when using these functions.
Proxy client
In addition to the above react-query helpers, the context also exposes your tRPC proxy client. This lets you call your procedures with async/await without needing to create an additional vanilla client.
tsx
tsx
Query Invalidation
You invalidate queries via the invalidate helper. invalidate is actually a special helper given that, unlike the other helpers, it's available at every level of the router map. This means you can either run invalidate on a single query, a whole router, or every router if you want. We get more in detail in the sections below.
Invalidating a single query
You can invalidate a query relating to a single procedure and even filter based on the input passed to it to prevent unnecessary calls to the back end.
Example code
tsx
tsx
Invalidating across whole routers
It is also possible to invalidate queries across an entire router rather then just one query.
Example code
Backend code
server/routers/_app.tstsx
server/routers/_app.tstsx
tsx
tsx
Invalidate full cache on every mutation
We have prefixed this as unstable_ as it's a new API, but you're safe to use it! Read more.
Keeping track of exactly what queries a mutation should invalidate is hard, therefore, it can be a pragmatic solution to invalidate the full cache as a side-effect on any mutation. Since we have request batching, this invalidation will simply refetch all queries on the page you're looking at in one single request.
We have added a feature to help with this:
ts
ts
Additional Options
Aside from the query helpers, the object useContext returns also contains the following properties:
ts
ts