TanStack React Query
Quick example query
tsximport {useQuery } from '@tanstack/react-query';import {useTRPC } from './trpc';functionUsers () {consttrpc =useTRPC ();constgreetingQuery =useQuery (trpc .greeting .queryOptions ({name : 'Jerry' }));// greetingQuery.data === 'Hello Jerry'}
tsximport {useQuery } from '@tanstack/react-query';import {useTRPC } from './trpc';functionUsers () {consttrpc =useTRPC ();constgreetingQuery =useQuery (trpc .greeting .queryOptions ({name : 'Jerry' }));// greetingQuery.data === 'Hello Jerry'}
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.
tsxexport default functionBasics () {consttrpc =useTRPC ();constqueryClient =useQueryClient ();// Create QueryOptions which can be passed to query hooksconstmyQueryOptions =trpc .path .to .query .queryOptions ({ /** inputs */ })constmyQuery =useQuery (myQueryOptions )// or:// useSuspenseQuery(myQueryOptions)// useInfiniteQuery(myQueryOptions)// Create MutationOptions which can be passed to useMutationconstmyMutationOptions =trpc .path .to .mutation .mutationOptions ()constmyMutation =useMutation (myMutationOptions )// Create a QueryKey which can be used to manipulate many methods// on TanStack's QueryClient in a type-safe mannerconstmyQueryKey =trpc .path .to .query .queryKey ()constinvalidateMyQueryKey = () => {queryClient .invalidateQueries ({queryKey :myQueryKey })}return (// Your app herenull)}
tsxexport default functionBasics () {consttrpc =useTRPC ();constqueryClient =useQueryClient ();// Create QueryOptions which can be passed to query hooksconstmyQueryOptions =trpc .path .to .query .queryOptions ({ /** inputs */ })constmyQuery =useQuery (myQueryOptions )// or:// useSuspenseQuery(myQueryOptions)// useInfiniteQuery(myQueryOptions)// Create MutationOptions which can be passed to useMutationconstmyMutationOptions =trpc .path .to .mutation .mutationOptions ()constmyMutation =useMutation (myMutationOptions )// Create a QueryKey which can be used to manipulate many methods// on TanStack's QueryClient in a type-safe mannerconstmyQueryKey =trpc .path .to .query .queryKey ()constinvalidateMyQueryKey = () => {queryClient .invalidateQueries ({queryKey :myQueryKey })}return (// Your app herenull)}
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.
tsconstqueryOptions =trpc .path .to .query .queryOptions ({/** input */id : 'foo',},{// Any Tanstack React Query optionsstaleTime : 1000,},);
tsconstqueryOptions =trpc .path .to .query .queryOptions ({/** input */id : 'foo',},{// Any Tanstack React Query optionsstaleTime : 1000,},);
You can additionally provide a trpc object to the queryOptions function to provide tRPC request options to the client.
tsconstqueryOptions =trpc .path .to .query .queryOptions ({/** input */id : 'foo',},{trpc : {// Provide tRPC request options to the clientcontext : {// see https://trpc.io/docs/client/links#managing-context},},},);
tsconstqueryOptions =trpc .path .to .query .queryOptions ({/** input */id : 'foo',},{trpc : {// Provide tRPC request options to the clientcontext : {// see https://trpc.io/docs/client/links#managing-context},},},);
If you want to disable a query in a type safe way, you can use skipToken:
tsconstquery =useQuery (trpc .user .details .queryOptions (user ?.id &&project ?.id ? {userId :user .id ,projectId :project .id ,}:skipToken ,{staleTime : 1000,},),);
tsconstquery =useQuery (trpc .user .details .queryOptions (user ?.id &&project ?.id ? {userId :user .id ,projectId :project .id ,}:skipToken ,{staleTime : 1000,},),);
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 take 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.
tsconstinfiniteQueryOptions =trpc .path .to .query .infiniteQueryOptions ({/** input */},{// Any Tanstack React Query optionsgetNextPageParam : (lastPage ,pages ) =>lastPage .nextCursor ,},);
tsconstinfiniteQueryOptions =trpc .path .to .query .infiniteQueryOptions ({/** input */},{// Any Tanstack React Query optionsgetNextPageParam : (lastPage ,pages ) =>lastPage .nextCursor ,},);
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.
tsconstqueryKey =trpc .path .to .query .queryKey ();
tsconstqueryKey =trpc .path .to .query .queryKey ();
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:
tsconstqueryKey =trpc .router .pathKey ();
tsconstqueryKey =trpc .router .pathKey ();
Or even the root path to match all tRPC queries:
tsconstqueryKey =trpc .pathKey ();
tsconstqueryKey =trpc .pathKey ();
infiniteQueryKey - getting the infinite query key
Available for all query procedures that take a cursor input. Allows you to access the query key for an infinite query in a type-safe manner.
tsconstinfiniteQueryKey =trpc .path .to .query .infiniteQueryKey ({/** input */});
tsconstinfiniteQueryKey =trpc .path .to .query .infiniteQueryKey ({/** input */});
The result can be used with query client methods like getQueryData, setQueryData, invalidateQueries, etc.
ts// Get cached data for an infinite queryconstcachedData =queryClient .getQueryData (trpc .path .to .query .infiniteQueryKey ({cursor : 0 }),);// Set cached data for an infinite queryqueryClient .setQueryData (trpc .path .to .query .infiniteQueryKey ({cursor : 0 }),(data ) => {// Modify the datareturndata ;},);
ts// Get cached data for an infinite queryconstcachedData =queryClient .getQueryData (trpc .path .to .query .infiniteQueryKey ({cursor : 0 }),);// Set cached data for an infinite queryqueryClient .setQueryData (trpc .path .to .query .infiniteQueryKey ({cursor : 0 }),(data ) => {// Modify the datareturndata ;},);
queryFilter - creating query filters
Available for all query procedures. Allows creating query filters in a type-safe manner.
tsconstqueryFilter =trpc .path .to .query .queryFilter ({/** input */},{// Any Tanstack React Query filterpredicate : (query ) => {return !!query .state .data ;},},);
tsconstqueryFilter =trpc .path .to .query .queryFilter ({/** input */},{// Any Tanstack React Query filterpredicate : (query ) => {return !!query .state .data ;},},);
Like with query keys, if you want to run a filter across a whole router you can use pathFilter to target any sub-path.
tsconstqueryFilter =trpc .path .pathFilter ({// Any Tanstack React Query filterpredicate : (query ) => {return !!query .state .data ;},});
tsconstqueryFilter =trpc .path .pathFilter ({// Any Tanstack React Query filterpredicate : (query ) => {return !!query .state .data ;},});
Useful for creating filters that can be passed to client methods like queryClient.invalidateQueries etc.
infiniteQueryFilter - creating infinite query filters
Available for all query procedures that take a cursor input. Allows creating query filters for infinite queries in a type-safe manner.
tsconstinfiniteQueryFilter =trpc .path .to .query .infiniteQueryFilter ({/** input */},{// Any Tanstack React Query filterpredicate : (query ) => {return !!query .state .data ;},},);
tsconstinfiniteQueryFilter =trpc .path .to .query .infiniteQueryFilter ({/** input */},{// Any Tanstack React Query filterpredicate : (query ) => {return !!query .state .data ;},},);
Useful for creating filters that can be passed to client methods like queryClient.invalidateQueries etc.
tsawaitqueryClient .invalidateQueries (trpc .path .to .query .infiniteQueryFilter ({},{predicate : (query ) => {// Filter logic based on query statereturnquery .state .status === 'success';},},),);
tsawaitqueryClient .invalidateQueries (trpc .path .to .query .infiniteQueryFilter ({},{predicate : (query ) => {// Filter logic based on query statereturnquery .state .status === 'success';},},),);
mutationOptions - creating mutation options
Available for all mutation procedures. Provides a type-safe identity function for constructing options that can be passed to useMutation.
tsconstmutationOptions =trpc .path .to .mutation .mutationOptions ({// Any Tanstack React Query optionsonSuccess : (data ) => {// do something with the data},});
tsconstmutationOptions =trpc .path .to .mutation .mutationOptions ({// Any Tanstack React Query optionsonSuccess : (data ) => {// do something with the data},});
mutationKey - getting the mutation key
Available for all mutation procedures. Allows you to get the mutation key in a type-safe manner.
tsconstmutationKey =trpc .path .to .mutation .mutationKey ();
tsconstmutationKey =trpc .path .to .mutation .mutationKey ();
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 identity 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.
tsxfunctionSubscriptionExample () {consttrpc =useTRPC ();constsubscription =useSubscription (trpc .path .to .subscription .subscriptionOptions ({/** input */},{enabled : true,onStarted : () => {// do something when the subscription is started},onData : (data ) => {// you can handle the data here},onError : (error ) => {// you can handle the error here},onConnectionStateChange : (state ) => {// you can handle the connection state here},},),);// Or you can handle the state heresubscription .data ; // The lastly received datasubscription .error ; // The lastly received error/*** The current status of the subscription.* Will be one of: `'idle'`, `'connecting'`, `'pending'`, or `'error'`.** - `idle`: subscription is disabled or ended* - `connecting`: trying to establish a connection* - `pending`: connected to the server, receiving data* - `error`: an error occurred and the subscription is stopped*/subscription .status ;// Reset the subscription (if you have an error etc)subscription .reset ();return <>{/* ... */}</>;}
tsxfunctionSubscriptionExample () {consttrpc =useTRPC ();constsubscription =useSubscription (trpc .path .to .subscription .subscriptionOptions ({/** input */},{enabled : true,onStarted : () => {// do something when the subscription is started},onData : (data ) => {// you can handle the data here},onError : (error ) => {// you can handle the error here},onConnectionStateChange : (state ) => {// you can handle the connection state here},},),);// Or you can handle the state heresubscription .data ; // The lastly received datasubscription .error ; // The lastly received error/*** The current status of the subscription.* Will be one of: `'idle'`, `'connecting'`, `'pending'`, or `'error'`.** - `idle`: subscription is disabled or ended* - `connecting`: trying to establish a connection* - `pending`: connected to the server, receiving data* - `error`: an error occurred and the subscription is stopped*/subscription .status ;// Reset the subscription (if you have an error etc)subscription .reset ();return <>{/* ... */}</>;}
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// Without prefixes - these would collide!constauthQuery =useQuery (trpcAuth .list .queryOptions ()); // auth serviceconstbillingQuery =useQuery (trpcBilling .list .queryOptions ()); // billing service
tsx// Without prefixes - these would collide!constauthQuery =useQuery (trpcAuth .list .queryOptions ()); // auth serviceconstbillingQuery =useQuery (trpcBilling .list .queryOptions ()); // billing service
Enable the feature flag when creating your context:
utils/trpc.tstsx// [...]constbilling =createTRPCContext <BillingRouter , {keyPrefix : true }>();export constBillingProvider =billing .TRPCProvider ;export constuseBilling =billing .useTRPC ;export constcreateBillingClient = () =>createTRPCClient <BillingRouter >({links : [/* ... */],});constaccount =createTRPCContext <AccountRouter , {keyPrefix : true }>();export constAccountProvider =account .TRPCProvider ;export constuseAccount =account .useTRPC ;export constcreateAccountClient = () =>createTRPCClient <AccountRouter >({links : [/* ... */],});
utils/trpc.tstsx// [...]constbilling =createTRPCContext <BillingRouter , {keyPrefix : true }>();export constBillingProvider =billing .TRPCProvider ;export constuseBilling =billing .useTRPC ;export constcreateBillingClient = () =>createTRPCClient <BillingRouter >({links : [/* ... */],});constaccount =createTRPCContext <AccountRouter , {keyPrefix : true }>();export constAccountProvider =account .TRPCProvider ;export constuseAccount =account .useTRPC ;export constcreateAccountClient = () =>createTRPCClient <AccountRouter >({links : [/* ... */],});
App.tsxtsximport {useState } from 'react';import {QueryClient ,QueryClientProvider } from '@tanstack/react-query';import {BillingProvider ,AccountProvider ,createBillingClient ,createAccountClient ,} from './utils/trpc';// [...]export functionApp () {const [queryClient ] =useState (() => newQueryClient ());const [billingClient ] =useState (() =>createBillingClient ());const [accountClient ] =useState (() =>createAccountClient ());return (<QueryClientProvider client ={queryClient }><BillingProvider trpcClient ={billingClient }queryClient ={queryClient }keyPrefix ="billing"><AccountProvider trpcClient ={accountClient }queryClient ={queryClient }keyPrefix ="account"><div >{/* ... */}</div ></AccountProvider ></BillingProvider ></QueryClientProvider >);}
App.tsxtsximport {useState } from 'react';import {QueryClient ,QueryClientProvider } from '@tanstack/react-query';import {BillingProvider ,AccountProvider ,createBillingClient ,createAccountClient ,} from './utils/trpc';// [...]export functionApp () {const [queryClient ] =useState (() => newQueryClient ());const [billingClient ] =useState (() =>createBillingClient ());const [accountClient ] =useState (() =>createAccountClient ());return (<QueryClientProvider client ={queryClient }><BillingProvider trpcClient ={billingClient }queryClient ={queryClient }keyPrefix ="billing"><AccountProvider trpcClient ={accountClient }queryClient ={queryClient }keyPrefix ="account"><div >{/* ... */}</div ></AccountProvider ></BillingProvider ></QueryClientProvider >);}
components/MyComponent.tsxtsximport {useQuery } from '@tanstack/react-query';import {useBilling ,useAccount } from '../utils/trpc';// [...]export functionMyComponent () {constbilling =useBilling ();constaccount =useAccount ();constbillingList =useQuery (billing .list .queryOptions ());constaccountList =useQuery (account .list .queryOptions ());return (<div ><div >Billing: {JSON .stringify (billingList .data ?? null)}</div ><div >Account: {JSON .stringify (accountList .data ?? null)}</div ></div >);}
components/MyComponent.tsxtsximport {useQuery } from '@tanstack/react-query';import {useBilling ,useAccount } from '../utils/trpc';// [...]export functionMyComponent () {constbilling =useBilling ();constaccount =useAccount ();constbillingList =useQuery (billing .list .queryOptions ());constaccountList =useQuery (account .list .queryOptions ());return (<div ><div >Billing: {JSON .stringify (billingList .data ?? null)}</div ><div >Account: {JSON .stringify (accountList .data ?? null)}</div ></div >);}
The query keys will be properly prefixed to avoid collisions:
tsx// Example of how the query keys look with prefixesconstqueryKeys = [[['billing'], ['list'], {type : 'query' }],[['account'], ['list'], {type : 'query' }],];
tsx// Example of how the query keys look with prefixesconstqueryKeys = [[['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
tsimport type {inferRouterInputs ,inferRouterOutputs } from '@trpc/server';import type {AppRouter } from './server/router';export typeInputs =inferRouterInputs <AppRouter >;export typeOutputs =inferRouterOutputs <AppRouter >;
tsimport type {inferRouterInputs ,inferRouterOutputs } from '@trpc/server';import type {AppRouter } from './server/router';export typeInputs =inferRouterInputs <AppRouter >;export typeOutputs =inferRouterOutputs <AppRouter >;
Infer types for a single procedure
tsimport type {inferInput ,inferOutput } from '@trpc/tanstack-react-query';functionComponent () {consttrpc =useTRPC ();typeInput =inferInput <typeoftrpc .path .to .procedure >;typeOutput =inferOutput <typeoftrpc .path .to .procedure >;}
tsimport type {inferInput ,inferOutput } from '@trpc/tanstack-react-query';functionComponent () {consttrpc =useTRPC ();typeInput =inferInput <typeoftrpc .path .to .procedure >;typeOutput =inferOutput <typeoftrpc .path .to .procedure >;}
Accessing the tRPC client
If you used the setup with React Context, you can access the tRPC client using the useTRPCClient hook.
tsximport {useTRPCClient } from './trpc';async functionComponent () {consttrpcClient =useTRPCClient ();constresult = awaittrpcClient .getUser .query ({id : '1',});}
tsximport {useTRPCClient } from './trpc';async functionComponent () {consttrpcClient =useTRPCClient ();constresult = awaittrpcClient .getUser .query ({id : '1',});}
If you setup without React Context, you can import the global client instance directly instead.
tsimport {client } from './trpc';constresult = awaitclient .path .to .procedure .query ({/** input */id : 'foo',});
tsimport {client } from './trpc';constresult = awaitclient .path .to .procedure .query ({/** input */id : 'foo',});