Migrating from the classic React Client
There are a few approaches to migrate over, and this library is a significant departure from the classic client, so we're not expecting anybody to do it in one shot. But you will probably want to try a combination of...
Codemod migration
The codemod is a work in progress and we're looking for help to make it better. If you're interested in contributing to the codemod, please see Julius' comment here.
We're working on a codemod to help you migrate your existing codebase over to the new client. This is already available to try but we need your feedback and contributions to improve it. Codemods are very tricky to get right so we're looking for your help to make it as effective as possible.
Run our upgrade CLI:
shnpx @trpc/upgrade
shnpx @trpc/upgrade
When prompted, select the transforms Migrate Hooks to xxxOptions API and Migrate context provider setup.
Gradual migration
The new and classic clients are compatible with each other and can live together in the same application. This means you can start migrating by using the new client in new parts of your application, and gradually migrate over existing usage as you see fit. Most importantly, Query Keys are identical, which means you can use the new client and classic client together and still rely on TanStack Query's caching.
Migrating Queries
A classic query would look like this
tsximport {trpc } from './trpc';functionUsers () {constgreetingQuery =trpc .greeting .useQuery ({name : 'Jerry' });// greetingQuery.data === 'Hello Jerry'}
tsximport {trpc } from './trpc';functionUsers () {constgreetingQuery =trpc .greeting .useQuery ({name : 'Jerry' });// greetingQuery.data === 'Hello Jerry'}
and changes to
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'}
Migrating Invalidations and other QueryClient usages
A classic invalidation pattern would look like this
tsximport {trpc } from './trpc';functionUsers () {constutils =trpc .useUtils ();async functioninvalidateGreeting () {awaitutils .greeting .invalidate ({name : 'Jerry' });}}
tsximport {trpc } from './trpc';functionUsers () {constutils =trpc .useUtils ();async functioninvalidateGreeting () {awaitutils .greeting .invalidate ({name : 'Jerry' });}}
and changes to
tsximport {useQueryClient } from '@tanstack/react-query';import {useTRPC } from './trpc';functionUsers () {consttrpc =useTRPC ();constqueryClient =useQueryClient ();async functioninvalidateGreeting () {awaitqueryClient .invalidateQueries (trpc .greeting .queryFilter ({name : 'Jerry' }),);}}
tsximport {useQueryClient } from '@tanstack/react-query';import {useTRPC } from './trpc';functionUsers () {consttrpc =useTRPC ();constqueryClient =useQueryClient ();async functioninvalidateGreeting () {awaitqueryClient .invalidateQueries (trpc .greeting .queryFilter ({name : 'Jerry' }),);}}
This is the same for any QueryClient usage, instead of using tRPC's useUtils you can now follow the TanStack documentation directly
Migrating Mutations
A classic mutation might look like this
tsximport {trpc } from './trpc';functionUsers () {constcreateUserMutation =trpc .createUser .useMutation ();createUserMutation .mutate ({name : 'Jerry' });}
tsximport {trpc } from './trpc';functionUsers () {constcreateUserMutation =trpc .createUser .useMutation ();createUserMutation .mutate ({name : 'Jerry' });}
and changes to
tsximport {useMutation } from '@tanstack/react-query';import {useTRPC } from './trpc';functionUsers () {consttrpc =useTRPC ();constcreateUserMutation =useMutation (trpc .createUser .mutationOptions ());createUserMutation .mutate ({name : 'Jerry' });}
tsximport {useMutation } from '@tanstack/react-query';import {useTRPC } from './trpc';functionUsers () {consttrpc =useTRPC ();constcreateUserMutation =useMutation (trpc .createUser .mutationOptions ());createUserMutation .mutate ({name : 'Jerry' });}