TanStack React Query
Compared to our classic React Query Integration this client is simpler and more TanStack Query-native, providing factories for common TanStack React Query interfaces like QueryKeys, QueryOptions, and MutationOptions. We think it's the future and recommend using this over the classic client, read the announcement post for more information about this change.
Quick example query
tsx
tsx
Usage
The philosophy of this client is to provide thin and type-safe factories which work natively and type-safely with Tanstack React Query. This means just by following the autocompletes the client gives you, you can focus on building just with the knowledge the TanStack React Query docs provide.
tsx
tsx
The trpc object is fully type-safe and will provide autocompletes for all the procedures in your AppRouter. At the end of the proxy, the following methods are available:
queryOptions - querying data
Available for all query procedures. Provides a type-safe wrapper around Tanstack's queryOptions function. The first argument is the input for the procedure, and the second argument accepts any native Tanstack React Query options.
ts
ts
You can additionally provide a trpc object to the queryOptions function to provide tRPC request options to the client.
ts
ts
If you want to disable a query in a type safe way, you can use skipToken:
ts
ts
The result can be passed to useQuery or useSuspenseQuery hooks or query client methods like fetchQuery, prefetchQuery, prefetchInfiniteQuery, invalidateQueries, etc.
infiniteQueryOptions - querying infinite data
Available for all query procedures that takes a cursor input. Provides a type-safe wrapper around Tanstack's infiniteQueryOptions function. The first argument is the input for the procedure, and the second argument accepts any native Tanstack React Query options.
ts
ts
queryKey - getting the query key and performing operations on the query client
Available for all query procedures. Allows you to access the query key in a type-safe manner.
ts
ts
Since Tanstack React Query uses fuzzy matching for query keys, you can also create a partial query key for any sub-path to match all queries belonging to a router:
ts
ts
Or even the root path to match all tRPC queries:
ts
ts
queryFilter - creating query filters
Available for all query procedures. Allows creating query filters in a type-safe manner.
ts
ts
Like with query keys, if you want to run a filter across a whole router you can use pathFilter to target any sub-path.
ts
ts
Useful for creating filters that can be passed to client methods like queryClient.invalidateQueries etc.
mutationOptions - creating mutation options
Available for all mutation procedures. Provides a type-safe identify function for constructing options that can be passed to useMutation.
ts
ts
mutationKey - getting the mutation key
Available for all mutation procedures. Allows you to get the mutation key in a type-safe manner.
ts
ts
subscriptionOptions - creating subscription options
TanStack does not provide a subscription hook, so we continue to expose our own abstraction here which works with a standard tRPC subscription setup.
Available for all subscription procedures. Provides a type-safe identify function for constructing options that can be passed to useSubscription.
Note that you need to have either the httpSubscriptionLink or wsLink configured in your tRPC client to use subscriptions.
tsx
tsx
Query Key Prefixing
When using multiple tRPC providers in a single application (e.g., connecting to different backend services), queries with the same path will collide in the cache. You can prevent this by enabling query key prefixing.
tsx
tsx
Enable the feature flag when creating your context:
utils/trpc.tstsx
utils/trpc.tstsx
App.tsxtsx
App.tsxtsx
components/MyComponent.tsxtsx
components/MyComponent.tsxtsx
The query keys will be properly prefixed to avoid collisions:
tsxqueryKeys = [[['billing'], ['list'], {type : 'query' }],[['account'], ['list'], {type : 'query' }],];
tsxqueryKeys = [[['billing'], ['list'], {type : 'query' }],[['account'], ['list'], {type : 'query' }],];
Inferring Input and Output types
When you need to infer the input and output types for a procedure or router, there are 2 options available depending on the situation.
Infer the input and output types of a full router
ts
ts
Infer types for a single procedure
ts
ts
Accessing the tRPC client
If you used the setup with React Context, you can access the tRPC client using the useTRPCClient hook.
tsx
tsx
If you setup without React Context, you can import the global client instance directly instead.
ts
ts