Set up the React Query Integration
1. Install dependencies
The following dependencies should be installed
- npm
- yarn
- pnpm
- bun
bash
bash
bash
bash
bash
bash
bash
bash
2. Import your AppRouter
Import your AppRouter type into the client application. This type holds the shape of your entire API.
tsAppRouter } from '../server/router';
tsAppRouter } from '../server/router';
By using import type you ensure that the reference will be stripped at compile-time, meaning you don't inadvertently import server-side code into your client. For more information, see the Typescript docs.
3. Create tRPC hooks
Create a set of strongly-typed React hooks from your AppRouter type signature with createTRPCReact.
utils/trpc.tstscreateTRPCReact } from '@trpc/react-query';import type {AppRouter } from '../server/router';export consttrpc =createTRPCReact <AppRouter >();
utils/trpc.tstscreateTRPCReact } from '@trpc/react-query';import type {AppRouter } from '../server/router';export consttrpc =createTRPCReact <AppRouter >();
4. Add tRPC providers
Create a tRPC client, and wrap your application in the tRPC Provider, as below. You will also need to set up and connect React Query, which they document in more depth.
If you already use React Query in your application, you should re-use the QueryClient and QueryClientProvider you already have.
App.tsxtsx
App.tsxtsx
The reason for using useState in the creation of the queryClient and the TRPCClient, as opposed to declaring them outside of the component, is to ensure that each request gets a unique client when using SSR. If you use client side rendering then you can move them if you wish.
5. Fetch data
You can now use the tRPC React Query integration to call queries and mutations on your API.
pages/IndexPage.tsxtsx= trpc.getUser.useQuery({ id: 'id_bilbo' }); const userCreator = trpc.createUser.useMutation();return (<div><p>{userQuery.data?.name}</p><button onClick={() => userCreator.mutate({ name: 'Frodo' })}> Create Frodo</button></div>);}
pages/IndexPage.tsxtsx= trpc.getUser.useQuery({ id: 'id_bilbo' }); const userCreator = trpc.createUser.useMutation();return (<div><p>{userQuery.data?.name}</p><button onClick={() => userCreator.mutate ({ name: 'Frodo' })}> Create Frodo</button></div>);}