Prefetching with handles
On this page
A handle is a small, serializable reference to a query you started in a server component, a route loader, or at the top of a page. Pass it to a component, and the component reads the query, fully typed, without naming the procedure or repeating its input.
// Server component, route loader, or render: start the query and get a handle
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid });
// Component: no procedure, no input, fully typed
const const postQuery: UseSuspenseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery = useSuspenseQuery<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>(options: UseSuspenseQueryOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>, queryClient?: QueryClient): UseSuspenseQueryResult<...>
useSuspenseQuery(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}
props.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post));Handles fix a gap in the usual prefetch pattern: the server prefetches trpc.post.byId.queryOptions({ id }), and the client repeats the same procedure and input:
- If the inputs differ, the read misses the prefetched data and silently fetches again.
- The client can read a different procedure from the one the server prefetched.
- Nothing shows that a prefetch happened at all.
With handles, the client reads exactly what the server started. They work best when a page can start every query from its URL: see Start every query from the URL.
A handle carries the query's key and input, but no data, whichever method made it. The data lives in the cache: see Handles carry no data. The cache fills through TanStack Query's dehydrate and hydrate, as in the Server Components guide or TanStack Start's SSR query integration. In a server loader, return dehydrate(queryClient) with the handles, as in React Router.
The components that read handles are the same everywhere: with or without server components, and whether the handle came from a loader or from render.
Starting a query
You start a query in one of two ways. Choose for each query:
- Don't wait with
unstable_$prefetch. The page renders at once, and the data streams in later. - Wait with
unstable_$ensure. The page renders once the data is ready.
unstable_$fetch is a stricter form of waiting, for code that needs fresh data. See Waiting for fresh data.
Without waiting: unstable_$prefetch
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid });-
It returns a handle at once. The request runs in the background.
-
The page doesn't wait. It renders, and the data arrives when the request finishes.
-
If the cache has no data for the query yet, the component that reads the handle renders before the data arrives, so it shows a loading state. If the cache has data, the component shows it while the request runs. With
useSuspenseQueryand no cached data, it suspends: wrap it in<Suspense>to show a fallback in its place. WithuseQuery, it renders withstatus: 'pending', and with server rendering it can cause a hydration mismatch: see Reading withuseQuery.<const Suspense: React.ExoticComponent<React.SuspenseProps>SuspenseReact.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading post…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}> <Postfunction Post(props: { post: unstable_inferQueryHandle<TRPC["post"]["byId"]>; }): JSX.Elementpost={post: unstable_TRPCQueryHandle<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>post} /> </const post: unstable_TRPCQueryHandle<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>Without a boundary of your own, React shows the fallback of the nearest boundary above it, which may cover the whole page.
-
It never throws. If the request fails, the component that reads the handle gets the error, and the nearest error boundary shows it.
Use it for data the page can show a placeholder for: slow queries, content below the fold, secondary panels.
Waiting: unstable_$ensure
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid });- It returns a promise. The promise resolves to a handle once the cache has data for the query.
- The handle carries no data. To use the data in the loader, read it from the cache: see Using the data in a loader.
- The page waits for that promise, so the data is ready before anything renders.
- The component that reads the handle finds the data in the cache and renders it at once, with
useSuspenseQueryoruseQuery. It still needs a loading state for when the cache loses the data: see When the cache loses the data. - It rejects if the request fails, so you can handle the error where you start the query, for example to show a not-found page.
- If the cache already has stale data, it resolves at once and refetches in the background.
Use it for data the page can't render without, or data you need before rendering: the page title, a not-found check, a redirect.
Waiting for fresh data: unstable_$fetch
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$fetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$fetch({ id: stringid });- It works like
unstable_$ensure, with one difference: if the cache has stale data, it refetches and waits for the new data. - Use it when the code that starts the query reads the data and needs it fresh, for example a loader that redirects based on the post.
- In a browser loader, prefer
unstable_$ensure. With stale data,unstable_$ensurerenders the page at once and updates it when the refetch lands.unstable_$fetchholds up the navigation until the refetch is done. - TanStack Router hides this difference on a page it has visited: by default, it shows the page with its last loader data and reruns the loader in the background. To wait there too, as the demo below does, set the router's
defaultStaleReloadMode, or a loader'sstaleReloadMode, to'blocking'. - In server components, the two behave the same: the request's
QueryClientstarts empty, so both wait for the request.
Which to use
unstable_$prefetch | unstable_$ensure | unstable_$fetch | |
|---|---|---|---|
| Returns | A handle | A promise for a handle | A promise for a handle |
| Page waits for data | No | Yes | Yes |
| With stale data | Refetches in the background | Resolves at once | Waits for the refetch |
| Reader's first render | Loading state, or cached data | The data | The data |
| When the request fails | An error boundary shows the error | The promise rejects | The promise rejects |
TanStack Query equivalents
Each $ method makes one TanStack Query call with the procedure's queryOptions or infiniteQueryOptions, then returns a handle:
| tRPC | TanStack Query |
|---|---|
trpc.x.unstable_$prefetch(input, opts) | void queryClient.prefetchQuery(trpc.x.queryOptions(input, opts)) |
await trpc.x.unstable_$ensure(input, opts) | await queryClient.ensureQueryData({ ...trpc.x.queryOptions(input, opts), revalidateIfStale: true }) |
await trpc.x.unstable_$fetch(input, opts) | await queryClient.fetchQuery(trpc.x.queryOptions(input, opts)) |
unstable_$prefetchInfinite, unstable_$ensureInfinite, unstable_$fetchInfinite | prefetchInfiniteQuery, ensureInfiniteQueryData (with revalidateIfStale: true), fetchInfiniteQuery |
trpc.x.unstable_$prefetch(input) on useTRPC(), in render | usePrefetchQuery(trpc.x.queryOptions(input)), as a plain call instead of a hook |
trpc.unstable_$fromHandle(handle, opts) | trpc.x.queryOptions(handle.input, opts), with x taken from the handle |
The queryClient is the one the proxy was created with.
Combining both
A page can wait for some queries and stream others. Start every query first, then await. If you await before starting the rest, the requests run one after another:
// 1. Start the query you won't wait for
const const related: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post[];
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
related = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.related: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post[];
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
related.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post[]; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post[];
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid });
// 2. Then wait for the one you need
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid });Here the post is ready when the page renders, and the related posts stream in behind their own loading state.
Options
inputis optional when the procedure's input is optional, sotrpc.viewer.unstable_$prefetch()works.optstakesstaleTime,gcTime,retry,retryDelay,meta, andtrpcrequest options such ascontext.
Where to start queries
| Your app | Start queries in | With |
|---|---|---|
| Next.js App Router | Server components | The server proxy from trpc/server.tsx |
| TanStack Start, TanStack Router | Route loaders | The proxy in your router context |
| React Router | loader or clientLoader | See React Router |
| No loaders or server components, such as a SPA | The top of the page, as it renders | useTRPC() |
- In server components, the request's
QueryClientstarts empty, sounstable_$ensurealways waits for the request. - On
useTRPC(),unstable_$prefetchis safe to call during render: likeusePrefetchQuery, it starts a query only if the cache has no state for it. See Without a loader.
Start every query from the URL
A loader or server component runs before anything renders, and all it has is the URL: the route's params, its search params, and on the server, the session. If every query a page needs takes its input from those, every request starts the moment the navigation does. No component has to render first to find out what to fetch.
- Path params name what the page is about:
/posts/$slug. - Search params hold everything else that changes what a query fetches: filters, sort, page. Keep them out of component state. A loader can't see component state, so the query would start only after render, the back button wouldn't restore the view, and a shared link would open the default one.
/posts?sort=top&page=2 → trpc.post.list.unstable_$prefetch({ sort: 'top', page: 2 })
If the URL isn't enough, reshape the procedure
If a page can't start a query from the URL alone, change the procedure, not the page. A procedure that takes an id you only learn from another query forces a waterfall on every page that calls it. Two fixes:
- Take what the URL has. Accept the slug or route param, and look up the id on the server.
- Return the joined data. Return the post with its author, not an author id for the client to look up.
The server sits next to the database, so the extra lookup costs a query there, not another round trip from the browser.
Before: the page is /posts/$slug, but the comments and the author need ids that only the post has:
import { import zz } from 'zod';
import { const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db } from './db';
import { const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t } from './trpc';
export const const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}>>
appRouter = const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.router: RouterBuilder
<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}>(_: {
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}) => BuiltRouter<...>
router({
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>
post: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.router: RouterBuilder
<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>(_: {
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}) => BuiltRouter<...>
router({
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>
bySlug: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.procedure: ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>procedure
.ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.input<z.ZodObject<{
slug: z.ZodString;
}, z.core.$strip>>(schema: z.ZodObject<{
slug: z.ZodString;
}, z.core.$strip>): ProcedureBuilder<object, object, object, {
slug: string;
}, {
slug: string;
}, UnsetMarker, UnsetMarker, false, DefaultErrorShape>
input(import zz.function object<{
slug: z.ZodString;
}>(shape?: {
slug: z.ZodString;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueUnrecognizedKeys | z.core.$ZodIssueInvalidType<unknown>>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
slug: z.ZodString;
}, z.core.$strip>
object({ slug: z.ZodStringslug: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string() }))
.ProcedureBuilder<object, object, object, { slug: string; }, { slug: string; }, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.query<Post>(resolver: ProcedureResolver<object, object, object, {
slug: string;
}, UnsetMarker, Post>): QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>
query(({ input: {
slug: string;
}
input }) => const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.post: {
bySlug(slug: string): Promise<Post>;
}
post.function bySlug(slug: string): Promise<Post>bySlug(input: {
slug: string;
}
input.slug: stringslug)),
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>
comments: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.procedure: ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>procedure
.ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.input<z.ZodObject<{
postId: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}, z.core.$strip>>(schema: z.ZodObject<{
postId: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}, z.core.$strip>): ProcedureBuilder<object, object, object, {
postId: string;
cursor?: number | null | undefined;
}, {
postId: string;
cursor?: number | null | undefined;
}, UnsetMarker, UnsetMarker, false, DefaultErrorShape>
input(
import zz.function object<{
postId: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}>(shape?: {
postId: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueUnrecognizedKeys | z.core.$ZodIssueInvalidType<unknown>>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
postId: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}, z.core.$strip>
object({ postId: z.ZodStringpostId: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string(), cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>cursor: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber().ZodType<any, any, $ZodNumberInternals<number>>.nullish(): z.ZodOptional<z.ZodNullable<z.ZodNumber>>nullish() }),
)
.ProcedureBuilder<object, object, object, { postId: string; cursor?: number | null | undefined; }, { postId: string; cursor?: number | null | undefined; }, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.query<{
items: Comment[];
nextCursor: number | null;
}>(resolver: ProcedureResolver<object, object, object, {
postId: string;
cursor?: number | null | undefined;
}, UnsetMarker, {
items: Comment[];
nextCursor: number | null;
}>): QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>
query(({ input: {
postId: string;
cursor?: number | null | undefined;
}
input }) =>
const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
}
comment.function list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>
list({
postId: stringpostId: input: {
postId: string;
cursor?: number | null | undefined;
}
input.postId: stringpostId,
cursor: number | nullcursor: input: {
postId: string;
cursor?: number | null | undefined;
}
input.cursor?: number | null | undefinedcursor ?? null,
}),
),
}),
author: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>
author: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.router: RouterBuilder
<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>;
}>(_: {
byId: QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>;
}) => BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>
router({
byId: QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>
byId: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.procedure: ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>procedure
.ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.input<z.ZodObject<{
id: z.ZodString;
}, z.core.$strip>>(schema: z.ZodObject<{
id: z.ZodString;
}, z.core.$strip>): ProcedureBuilder<object, object, object, {
id: string;
}, {
id: string;
}, UnsetMarker, UnsetMarker, false, DefaultErrorShape>
input(import zz.function object<{
id: z.ZodString;
}>(shape?: {
id: z.ZodString;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueUnrecognizedKeys | z.core.$ZodIssueInvalidType<unknown>>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
id: z.ZodString;
}, z.core.$strip>
object({ id: z.ZodStringid: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string() }))
.ProcedureBuilder<object, object, object, { id: string; }, { id: string; }, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.query<Author>(resolver: ProcedureResolver<object, object, object, {
id: string;
}, UnsetMarker, Author>): QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>
query(({ input: {
id: string;
}
input }) => const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.author: {
byId(id: string): Promise<Author>;
}
author.function byId(id: string): Promise<Author>byId(input: {
id: string;
}
input.id: stringid)),
}),
});async function function loader(params: {
slug: string;
}): Promise<{
post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
author: unstable_TRPCQueryHandle<...>;
}>
loader(params: {
slug: string;
}
params: { slug: stringslug: string }) {
const const post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.bySlug: DecorateQueryProcedure<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
bySlug.DecorateQueryHandleVerbs<{ input: { slug: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input: {
slug: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({
slug: stringslug: params: {
slug: string;
}
params.slug: stringslug,
});
// Nothing below can start until the post has arrived
const { const id: stringid, const authorId: stringauthorId } = await const queryClient: QueryClientqueryClient.QueryClient.ensureQueryData<Post, ErrorOf<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>(options: EnsureQueryDataOptions<Post, ErrorOf<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix, never>): Promise<...>
ensureQueryData(
const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post),
);
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; cursor?: number | null | undefined; }; output: { items: Comment[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({
postId: stringpostId: const id: stringid,
});
const const author: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Author;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
author = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
author: BuiltRouter<...>;
}>>, {
...;
}>
trpc.author: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Author;
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
author.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Author;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Author; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Author;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid: const authorId: stringauthorId });
return { post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post, comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments, author: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Author;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
author };
}post.bySlug |-------|
post.comments |-------|
author.byId |-------|
After: post.comments takes the slug, and post.bySlug returns the post with its author:
import { import zz } from 'zod';
import { const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db } from './db';
import { const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t } from './trpc';
export const const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>
appRouter = const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.router: RouterBuilder
<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>(_: {
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}) => BuiltRouter<...>
router({
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>
post: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.router: RouterBuilder
<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>(_: {
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}) => BuiltRouter<...>
router({
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>
bySlug: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.procedure: ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>procedure
.ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.input<z.ZodObject<{
slug: z.ZodString;
}, z.core.$strip>>(schema: z.ZodObject<{
slug: z.ZodString;
}, z.core.$strip>): ProcedureBuilder<object, object, object, {
slug: string;
}, {
slug: string;
}, UnsetMarker, UnsetMarker, false, DefaultErrorShape>
input(import zz.function object<{
slug: z.ZodString;
}>(shape?: {
slug: z.ZodString;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueUnrecognizedKeys | z.core.$ZodIssueInvalidType<unknown>>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
slug: z.ZodString;
}, z.core.$strip>
object({ slug: z.ZodStringslug: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string() }))
.ProcedureBuilder<object, object, object, { slug: string; }, { slug: string; }, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.query<{
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
}>(resolver: ProcedureResolver<object, object, object, {
slug: string;
}, UnsetMarker, {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
}>): QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>
query(async ({ input: {
slug: string;
}
input }) => {
const const post: Postpost = await const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.post: {
bySlug(slug: string): Promise<Post>;
}
post.function bySlug(slug: string): Promise<Post>bySlug(input: {
slug: string;
}
input.slug: stringslug);
return { ...const post: Postpost, author: Authorauthor: await const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.author: {
byId(id: string): Promise<Author>;
}
author.function byId(id: string): Promise<Author>byId(const post: Postpost.authorId: stringauthorId) };
}),
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>
comments: const t: TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, {
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}>
t.TRPCRootObject<object, object, RuntimeConfigOptions<object, object>, { ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }>.procedure: ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>procedure
.ProcedureBuilder<object, object, object, UnsetMarker, UnsetMarker, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.input<z.ZodObject<{
slug: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}, z.core.$strip>>(schema: z.ZodObject<{
slug: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}, z.core.$strip>): ProcedureBuilder<object, object, object, {
slug: string;
cursor?: number | null | undefined;
}, {
slug: string;
cursor?: number | null | undefined;
}, UnsetMarker, UnsetMarker, false, DefaultErrorShape>
input(
import zz.function object<{
slug: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}>(shape?: {
slug: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueUnrecognizedKeys | z.core.$ZodIssueInvalidType<unknown>>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
slug: z.ZodString;
cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
}, z.core.$strip>
object({ slug: z.ZodStringslug: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string(), cursor: z.ZodOptional<z.ZodNullable<z.ZodNumber>>cursor: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber().ZodType<any, any, $ZodNumberInternals<number>>.nullish(): z.ZodOptional<z.ZodNullable<z.ZodNumber>>nullish() }),
)
.ProcedureBuilder<object, object, object, { slug: string; cursor?: number | null | undefined; }, { slug: string; cursor?: number | null | undefined; }, UnsetMarker, UnsetMarker, false, DefaultErrorShape>.query<{
items: Comment[];
nextCursor: number | null;
}>(resolver: ProcedureResolver<object, object, object, {
slug: string;
cursor?: number | null | undefined;
}, UnsetMarker, {
items: Comment[];
nextCursor: number | null;
}>): QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>
query(async ({ input: {
slug: string;
cursor?: number | null | undefined;
}
input }) => {
const const post: Postpost = await const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.post: {
bySlug(slug: string): Promise<Post>;
}
post.function bySlug(slug: string): Promise<Post>bySlug(input: {
slug: string;
cursor?: number | null | undefined;
}
input.slug: stringslug);
return const db: {
post: {
bySlug(slug: string): Promise<Post>;
};
author: {
byId(id: string): Promise<Author>;
};
comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
};
}
db.comment: {
list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>;
}
comment.function list(opts: {
postId: string;
cursor: number | null;
}): Promise<{
items: Comment[];
nextCursor: number | null;
}>
list({
postId: stringpostId: const post: Postpost.id: stringid,
cursor: number | nullcursor: input: {
slug: string;
cursor?: number | null | undefined;
}
input.cursor?: number | null | undefinedcursor ?? null,
});
}),
}),
});async function function loader(params: {
slug: string;
}): Promise<{
post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>
loader(params: {
slug: string;
}
params: { slug: stringslug: string }) {
// Both start from the URL, together
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { slug: string; cursor?: number | null | undefined; }; output: { items: Comment[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$prefetchInfinite: (input: {
slug: string;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({
slug: stringslug: params: {
slug: string;
}
params.slug: stringslug,
});
const const post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
bySlug: QueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.bySlug: DecorateQueryProcedure<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
bySlug.DecorateQueryHandleVerbs<{ input: { slug: string; }; output: { author: Author; id: string; slug: string; title: string; authorId: string; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$ensure: (input: {
slug: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({
slug: stringslug: params: {
slug: string;
}
params.slug: stringslug,
});
return { post: unstable_TRPCQueryHandle<{
input: {
slug: string;
};
output: {
author: Author;
id: string;
slug: string;
title: string;
authorId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post, comments: unstable_TRPCInfiniteQueryHandle<{
input: {
slug: string;
cursor?: number | null | undefined;
};
output: {
items: Comment[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments };
}post.bySlug |-------|
post.comments |-------|
The page is ready after one round trip instead of two, and the loader no longer reads the post to start the other queries.
Reading a handle
trpc.unstable_$fromHandle(handle, opts?) returns the same options as trpc.<path>.queryOptions(input, opts) for the handle's procedure and input, or trpc.<path>.infiniteQueryOptions(input, opts) for an infinite handle. Pass them to any TanStack Query hook or QueryClient method:
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = function useTRPC(): TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC();
const const postQuery: UseSuspenseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery = useSuspenseQuery<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>(options: UseSuspenseQueryOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>, queryClient?: QueryClient): UseSuspenseQueryResult<...>
useSuspenseQuery(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const props: {
post: PostHandle;
a: PostHandle;
b: PostHandle;
}
props.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post));
const const titleQuery: UseQueryResult<string, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
titleQuery = useQuery<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, string, TRPCQueryKeyWithoutPrefix>(options: UndefinedInitialDataOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, string, TRPCQueryKeyWithoutPrefix>, queryClient?: QueryClient): UseQueryResult<...> (+2 overloads)
useQuery(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, string>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, string, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const props: {
post: PostHandle;
a: PostHandle;
b: PostHandle;
}
props.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post, { select?: ((data: NoInfer<Post>) => string) | undefinedselect: (post: NoInfer<Post>post) => post: Postpost.title: stringtitle }));
const [const aQuery: UseSuspenseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
aQuery, const bQuery: UseSuspenseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
bQuery] = useSuspenseQueries<[UnusedSkipTokenTRPCQueryOptionsOut<Post, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
keyPrefix: false;
}>, UnusedSkipTokenTRPCQueryOptionsOut<Post, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
keyPrefix: false;
}>], [...]>(options: {
...;
}, queryClient?: QueryClient): [...] (+1 overload)
useSuspenseQueries({
queries: readonly [UseSuspenseQueryOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>, UseSuspenseQueryOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>]
queries: [const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const props: {
post: PostHandle;
a: PostHandle;
b: PostHandle;
}
props.a: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
a), const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const props: {
post: PostHandle;
a: PostHandle;
b: PostHandle;
}
props.b: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
b)],
});optstakes everythingqueryOptionstakes, such asselect,staleTime,refetchIntervalandtrpc.unstable_$fromHandleis available on every router level:trpc.unstable_$fromHandleandtrpc.post.unstable_$fromHandlebehave the same.- Reads use the same query key as
trpc.<path>.queryOptions(input), so old and new code share cache entries. handle.queryKeyis the cache entry's key. For a typedqueryClient.getQueryData, passtrpc.unstable_$fromHandle(handle).queryKey. The handle's own key isn't tagged with its data type, so the handle stays serializable everywhere, including TanStack Router'sbeforeLoad.handle.inputis the typed input the query was started with, so a component doesn't need it as a separate prop. Infinite handles leave outcursoranddirection.
Typing props
Export your proxy type once, then type props with unstable_inferQueryHandle:
export type type TRPC = DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
TRPC = type TRPCOptionsProxy<TRouter extends AnyTRPCRouter, TFeatureFlags extends FeatureFlags = { keyPrefix: false; }> = DecoratedRouterRecord<TRouter["_def"]["_config"]["$types"], TRouter["_def"]["record"], TFeatureFlags> & DecorateRouterKeyable<TFeatureFlags> & DecorateHandleReaders<TFeatureFlags>TRPCOptionsProxy<type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter>;'use client';
import { function useSuspenseQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends QueryKey = readonly unknown[]>(options: UseSuspenseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseSuspenseQueryResult<TData, TError>useSuspenseQuery } from '@tanstack/react-query';
import type { type unstable_inferQueryHandle<TProcedure extends DecorateQueryHandleVerbs<any>> = TProcedure["unstable_$prefetch"] extends (...args: any) => infer R ? R : anyunstable_inferQueryHandle } from '@trpc/tanstack-react-query';
import { const useTRPC: () => TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC, type type TRPC = DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
TRPC } from '~/trpc/client';
export function function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}
props: { post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post: type unstable_inferQueryHandle<TProcedure extends DecorateQueryHandleVerbs<any>> = TProcedure["unstable_$prefetch"] extends (...args: any) => infer R ? R : anyunstable_inferQueryHandle<type TRPC = DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
TRPC['post']['byId']> }) {
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = function useTRPC(): TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC();
const const postQuery: UseSuspenseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery = useSuspenseQuery<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>(options: UseSuspenseQueryOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>, queryClient?: QueryClient): UseSuspenseQueryResult<...>
useSuspenseQuery(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}
props.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post));
return <React.JSX.IntrinsicElements.h1: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h1>{const postQuery: UseSuspenseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery.data: Postdata.title: stringtitle}</React.JSX.IntrinsicElements.h1: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h1>;
}A handle can only come from unstable_$prefetch, unstable_$ensure or unstable_$fetch. A raw input, or an object with the right queryKey and input written by hand, doesn't type-check. In tests, start a handle against a test QueryClient instead: see Testing components that take handles. At runtime, unstable_$fromHandle also throws a readable error for anything that isn't a handle, and when the handle's keyPrefix differs from the reading proxy's.
unstable_inferQueryHandle accepts a handle from any of the three methods: they all return the same type.
Reading with useQuery
You don't need useSuspenseQuery or a Suspense boundary to read a handle. useQuery works with every method, as for any query. Switch on its status to show each state:
'use client';
import { function useQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends QueryKey = readonly unknown[]>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): DefinedUseQueryResult<TData, TError> (+2 overloads)useQuery } from '@tanstack/react-query';
import type { type unstable_inferQueryHandle<TProcedure extends DecorateQueryHandleVerbs<any>> = TProcedure["unstable_$prefetch"] extends (...args: any) => infer R ? R : anyunstable_inferQueryHandle } from '@trpc/tanstack-react-query';
import { const useTRPC: () => TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC, type type TRPC = DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
TRPC } from '~/trpc/client';
export function function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}
props: { post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post: type unstable_inferQueryHandle<TProcedure extends DecorateQueryHandleVerbs<any>> = TProcedure["unstable_$prefetch"] extends (...args: any) => infer R ? R : anyunstable_inferQueryHandle<type TRPC = DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
TRPC['post']['byId']> }) {
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = function useTRPC(): TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC();
const const postQuery: UseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery = useQuery<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>(options: UndefinedInitialDataOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>, queryClient?: QueryClient): UseQueryResult<...> (+2 overloads)
useQuery(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}
props.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post));
switch (const postQuery: UseQueryResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery.status: "pending" | "error" | "success"status) {
case 'pending':
return <React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading post…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>;
case 'error':
return <React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Couldn't load the post: {const postQuery: QueryObserverRefetchErrorResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>> | QueryObserverLoadingErrorResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery.error: ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
error.TRPCClientErrorBase<TShape extends DefaultErrorShape>.message: stringmessage}</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>;
case 'success':
return <React.JSX.IntrinsicElements.h1: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h1>{const postQuery: QueryObserverSuccessResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>> | QueryObserverPlaceholderResult<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
postQuery.data: Postdata.title: stringtitle}</React.JSX.IntrinsicElements.h1: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h1>;
}
}useQuerydoesn't suspend. Until the data arrives, it renders withstatus: 'pending'.- After
unstable_$ensureorunstable_$fetch, the cache has the data, so the first render hasstatus: 'success'. - With server rendering, read an
unstable_$prefetchhandle withuseSuspenseQuery: withuseQuery, the server can render the loading state while the browser already has the data, and React reports a hydration mismatch (TanStack/query#9399). For a component onuseQuery, wait for the data instead:unstable_$ensure.
Handles carry no data
A handle is only the query's key and input, whichever method made it. The data lives in the cache, so the reader decides how to wait for it:
useSuspenseQuerytypesdataas defined. The component suspends until the cache has the data.useQuerytypesdataas possiblyundefined, as for any query. Switch onstatusto narrow it.
When the cache loses the data
Waiting for a query with unstable_$ensure or unstable_$fetch puts its data in the cache before the reader renders. The cache can still lose that data later, and the reader then has to load it again:
queryClient.resetQueries()clears the data and refetches the queries on screen at once. AuseSuspenseQueryreader suspends and shows the nearestSuspensefallback. AuseQueryreader renders withstatus: 'pending'.queryClient.removeQueries()andqueryClient.clear(), for example on logout, drop the query from the cache. A reader on screen keeps its data until it renders again. Then it loads the query as above.- TanStack Query also drops a query no component has used for its
gcTime. A reader that mounts after that loads it again. queryClient.invalidateQueries()keeps the data and refetches in the background, so the reader shows no loading state.
So give every reader a loading state, even when the page waited for its data: put a useSuspenseQuery reader under a Suspense boundary, or handle 'pending' in a useQuery reader. Without a boundary of its own, a reader that suspends shows the fallback of the nearest boundary above it, which may cover the whole page.
The reader loads the query again with a new request. A handle doesn't bring back the data the loader started with.
This is how readers behave with the default options. Some options change it: with initialData, resetQueries() puts that data back, and with enabled: false, a reader that lost its data stays pending and starts no request.
Using the data in a loader
To use the data where you started the query, for example to set the page title or redirect, read it from the cache. After unstable_$ensure, ensureQueryData returns it without a request:
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid });
const data = await function getQueryClient(): QueryClientgetQueryClient().QueryClient.ensureQueryData<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix>(options: EnsureQueryDataOptions<Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, Post, TRPCQueryKeyWithoutPrefix, never>): Promise<...>
ensureQueryData(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, Post>(handle: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<Post>, Post, ErrorOf<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post));If the data only decides which query to start next, try to start that query from the URL instead, as in Start every query from the URL. Otherwise, see Dependent queries.
Infinite queries
Procedures with a cursor input also have unstable_$prefetchInfinite, unstable_$ensureInfinite and unstable_$fetchInfinite. They load the first page. To load more pages up front, pass pages with getNextPageParam:
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: const id: stringid, limit?: number | undefinedlimit: 20 });
const const firstThree: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
firstThree = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$ensureInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => Promise<unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensureInfinite(
{ postId: stringpostId: const id: stringid, limit?: number | undefinedlimit: 20 },
{ pages: numberpages: 3, getNextPageParam: HandleGetNextPageParam<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
getNextPageParam: (last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last) => last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last.nextCursor: number | nullnextCursor },
);Functions can't cross to the client, so pass getNextPageParam when you read an infinite handle:
const const commentsQuery: UseSuspenseInfiniteQueryResult<TRPCInfiniteData<{
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>, ErrorOf<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
commentsQuery = useSuspenseInfiniteQuery<{
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}, ErrorOf<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, TRPCInfiniteData<{
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>, TRPCQueryKeyWithoutPrefix, number | null>(options: UseSuspenseInfiniteQueryOptions<...>, queryClient?: QueryClient): UseSuspenseInfiniteQueryResult<...>
useSuspenseInfiniteQuery(
const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>(handle: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts: UnusedSkipTokenTRPCInfiniteQueryOptionsIn<...>) => UnusedSkipTokenTRPCInfiniteQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}
props.comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments, {
getNextPageParam: GetNextPageParamFunction<number | null, NoInfer<{
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>>
getNextPageParam: (last: NoInfer<{
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>
last) => last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last.nextCursor: number | nullnextCursor,
}),
);Type the prop with unstable_inferInfiniteQueryHandle<TRPC['post']['comments']>.
In the browser, a loader can run again while the cache holds every page the user loaded. If that data is stale, the verb refetches all of those pages, which needs getNextPageParam from page two on. Pass the reader's getNextPageParam to loaders that run in the browser:
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite(
{ postId: stringpostId: const id: stringid, limit?: number | undefinedlimit: 20 },
{ getNextPageParam: (last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}) => number | null
getNextPageParam: (last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last) => last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last.nextCursor: number | nullnextCursor },
);Changing the input when you read
A handle's read always uses the handle's input. To change part of it in the component, read handle.input and build the options yourself:
import { function useSuspenseInfiniteQuery<TQueryFnData, TError = Error, TData = InfiniteData<TQueryFnData, unknown>, TQueryKey extends QueryKey = readonly unknown[], TPageParam = unknown>(options: UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): UseSuspenseInfiniteQueryResult<TData, TError>useSuspenseInfiniteQuery } from '@tanstack/react-query';
import type { type unstable_inferInfiniteQueryHandle<TProcedure extends DecorateInfiniteQueryHandleVerbs<any>> = TProcedure["unstable_$prefetchInfinite"] extends (...args: any) => infer R ? R : anyunstable_inferInfiniteQueryHandle } from '@trpc/tanstack-react-query';
import { function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>] (+1 overload)useState } from 'react';
export function function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<typeof serverTRPC.post.comments>;
}): JSX.Element
Comments(props: {
comments: unstable_inferInfiniteQueryHandle<typeof serverTRPC.post.comments>;
}
props: {
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments: type unstable_inferInfiniteQueryHandle<TProcedure extends DecorateInfiniteQueryHandleVerbs<any>> = TProcedure["unstable_$prefetchInfinite"] extends (...args: any) => infer R ? R : anyunstable_inferInfiniteQueryHandle<typeof const serverTRPC: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
comments: QueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
serverTRPC.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
comments: QueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments>;
}) {
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
comments: QueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc = const useTRPC: () => TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
comments: QueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
useTRPC();
const [const sort: "new" | "top"sort, const setSort: React.Dispatch<React.SetStateAction<"new" | "top">>setSort] = useState<"new" | "top">(initialState: "new" | "top" | (() => "new" | "top")): ["new" | "top", React.Dispatch<React.SetStateAction<"new" | "top">>] (+1 overload)useState(props: {
comments: unstable_inferInfiniteQueryHandle<typeof serverTRPC.post.comments>;
}
props.comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.unstable_TRPCInfiniteQueryHandle<{ input: { postId: string; sort: "new" | "top"; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.input: Omit<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, "cursor" | "direction">
input.sort: "new" | "top"sort);
const const commentsQuery: UseSuspenseInfiniteQueryResult<TRPCInfiniteData<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>, TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>>
commentsQuery = useSuspenseInfiniteQuery<{
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}, TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>, TRPCInfiniteData<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>, TRPCQueryKeyWithoutPrefix, number | null>(options: UseSuspenseInfiniteQueryOptions<{
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}, TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>, TRPCInfiniteData<...>, TRPCQueryKeyWithoutPrefix, number | null>, queryClient?: QueryClient): UseSuspenseInfiniteQueryResult<...>
useSuspenseInfiniteQuery(
const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
comments: QueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
comments: QueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryProcedure<{ input: { postId: string; sort: "new" | "top"; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.infiniteQueryOptions: TRPCInfiniteQueryOptions
<{
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>(input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, opts: UnusedSkipTokenTRPCInfiniteQueryOptionsIn<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}, TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>, {
...;
}>) => UnusedSkipTokenTRPCInfiniteQueryOptionsOut<...> (+2 overloads)
infiniteQueryOptions(
{ ...props: {
comments: unstable_inferInfiniteQueryHandle<typeof serverTRPC.post.comments>;
}
props.comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.unstable_TRPCInfiniteQueryHandle<{ input: { postId: string; sort: "new" | "top"; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.input: Omit<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, "cursor" | "direction">
input, sort: "new" | "top"sort },
{ getNextPageParam: GetNextPageParamFunction<number | null, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>
getNextPageParam: (last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last) => last: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}
last.nextCursor: number | nullnextCursor },
),
);
return (
<>
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() => const setSort: (value: React.SetStateAction<"new" | "top">) => voidsetSort('top')}>Top</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>
<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>{const commentsQuery: UseSuspenseInfiniteQueryResult<TRPCInfiniteData<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>, TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>>
commentsQuery.data: TRPCInfiniteData<{
postId: string;
sort: "new" | "top";
cursor?: number | null | undefined;
}, {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}>
data.InfiniteData<{ items: { id: string; text: string; }[]; nextCursor: number | null; }, number | null>.pages: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
}[]
pages[0]?.items: {
id: string;
text: string;
}[]
items.Array<{ id: string; text: string; }>.length: numberlength} comments</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>
</>
);
}- The handle's input still says which procedure and input the loader started, so the first render reads the loader's query.
- The new query starts when the component renders with the new input, so its first page is a waterfall. Wrap
setSortinstartTransitionto keep showing the old list while it loads.
Without a loader: in render
Not every app has a route loader or server components to start queries in. A single-page app, or a page whose input lives in component state, can start its queries at the top of the page as it renders, and pass the handles down:
import { const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense } from 'react';
import { const useTRPC: () => TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC } from '~/trpc/client';
import { function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments } from './comments';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from './post';
export function function PostPage(props: {
id: string;
}): JSX.Element
PostPage(props: {
id: string;
}
props: { id: stringid: string }) {
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = function useTRPC(): TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC();
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid: props: {
id: string;
}
props.id: stringid });
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: props: {
id: string;
}
props.id: stringid, limit?: number | undefinedlimit: 20 });
return (
<>
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading post…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading comments…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments={const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
</>
);
}Post and Comments are the same components a loader or a server component would feed. Both queries start before either of them renders, so they load in parallel instead of one after the other.
- On
useTRPC(),unstable_$prefetchandunstable_$prefetchInfinitefollow the rule of TanStack Query'susePrefetchQuery: they start a query only if the cache has no entry for it, with no data, no error and no request in flight. The cache looks it up by TanStack Query's query hash, which ignores the order of keys in the input, so re-renders with the same input start nothing. When an input changes, the new query starts. - They're plain calls, not hooks: you can call them conditionally, and React Compiler can memoize them.
- A query with cached data renders it at once. The component that reads the handle refetches it in the background if it's stale, as
useQueryanduseSuspenseQuerydo by default. - A render can't wait, so on
useTRPC(),unstable_$ensureandunstable_$fetchthrow. Read the handle withuseSuspenseQueryto suspend until the data arrives, or withuseQueryto show a loading state. - Start queries high in the tree: in the page, not in the components that read the handles. Starting queries where they're read brings back the waterfall.
- With server rendering, the queries start in the server render too. TanStack Start's SSR query integration streams them to the browser. Elsewhere, such as React Router's framework mode, nothing sends their data to the browser, which fetches them again: start them in a loader instead.
- Only the proxy from
useTRPC()behaves this way. A proxy fromcreateTRPCOptionsProxyis for loaders and server components, whereunstable_$prefetchrefetches stale data likequeryClient.prefetchQuery. During render, it would refetch on every render.
Request waterfalls
A request waterfall is a request that can't start until another one has finished. Each level costs a full round trip, and on a slow connection a round trip can take hundreds of milliseconds. Removing waterfalls is often the best way to speed up a page.
Handles are built around this. Queries start in loaders and server components, where everything a page needs is known up front, so they start together:
1. |-> post.byId
1. |-> post.comments
1. |-> user.byId
instead of one after the other, as each component renders and finds out what it needs:
1. |-> post.byId
2. |-> post.comments
3. |-> user.byId
The demo runs one page all three ways. Watch the network panel:
To keep it that way:
- Start queries in loaders and server components, not in render.
- Start every query before awaiting any. See Combining both.
- Make the URL the input, so the loader can start every query. See Start every query from the URL.
Dependent queries
A query whose input comes from another query's result is a waterfall by construction. Most pages don't need one: put the input in the route, or reshape the procedure so the page can start it from the URL. For example, trpc.viewer.org instead of trpc.viewer and then trpc.org.byId.
When the data really can't be reshaped, for example a third-party API you don't control, there are two explicit ways. Both pay the extra round trip.
In a loader or server component
Wait for the first query, read its data from the cache, then start the second:
const const viewer: unstable_TRPCQueryHandle<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
viewer = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.viewer: DecorateQueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
viewer.DecorateQueryHandleVerbs<{ input: void; output: { id: string; name: string; orgId: string; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input?: void | undefined, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure();
const { const orgId: stringorgId } = await function getQueryClient(): QueryClientgetQueryClient().QueryClient.ensureQueryData<{
id: string;
name: string;
orgId: string;
}, ErrorOf<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
id: string;
name: string;
orgId: string;
}, TRPCQueryKeyWithoutPrefix>(options: EnsureQueryDataOptions<{
id: string;
name: string;
orgId: string;
}, ErrorOf<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
id: string;
name: string;
orgId: string;
}, TRPCQueryKeyWithoutPrefix, never>): Promise<...>
ensureQueryData(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.DecorateHandleReaders<{ keyPrefix: false; }>.unstable_$fromHandle: <{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}, {
id: string;
name: string;
orgId: string;
}>(handle: unstable_TRPCQueryHandle<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, opts?: UnusedSkipTokenTRPCQueryOptionsIn<NoInfer<{
id: string;
name: string;
orgId: string;
}>, {
id: string;
name: string;
orgId: string;
}, ErrorOf<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>, {
...;
}> | undefined) => UnusedSkipTokenTRPCQueryOptionsOut<...> (+1 overload)
unstable_$fromHandle(const viewer: unstable_TRPCQueryHandle<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
viewer));
const const org: unstable_TRPCQueryHandle<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
org = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.org: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
org.byId: DecorateQueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { orgId: string; }; output: { id: string; name: string; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
orgId: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ orgId: stringorgId });The page waits for the first request, and the second starts only after it.
In a component
When an input is only known at render time, such as an id from a parent query's result, no loader can start the query. Start it in render in the component that has the input, and pass the handle to the one that renders it:
function function PostAuthor(props: {
post: Post;
}): JSX.Element
PostAuthor(props: {
post: Post;
}
props: { post: Postpost: type Post = {
id: string;
title: string;
authorId: string;
}
Post }) {
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = function useTRPC(): TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
useTRPC();
const const author: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
author = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.user: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
user.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: { id: string; name: string; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid: props: {
post: Post;
}
props.post: Postpost.authorId: stringauthorId });
return (
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<function AuthorSkeleton(): React.ReactNodeAuthorSkeleton />}>
<function AuthorCard(props: {
author: unstable_inferQueryHandle<TRPC["user"]["byId"]>;
}): React.ReactNode
AuthorCard author: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
author={const author: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: {
id: string;
name: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
author} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
);
}- The query starts when the component renders, an extra round trip after its parent's data arrives. Searching for
unstable_$prefetchin components below the top of a page lists these waterfalls. - Reading
trpc.user.byId.queryOptions(input)withuseSuspenseQueryworks too, as it always has. - In the Next.js App Router, start the dependent query in the server component instead, as in the loader example.
React Router
React Router loads data in one of two places, and handles fit each a little differently. Route components pass handles from loaderData to the same client components as everywhere else.
Client-only apps
In SPA Mode (ssr: false) with clientLoader, or in Data Mode with createBrowserRouter, loaders run in the browser. Create the tab's QueryClient and proxy in a module, so loaders can import them, and provide the same client to your app:
import { class QueryClientQueryClient } from '@tanstack/react-query';
import { function createTRPCClient<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter>): TRPCClient<TRouter>createTRPCClient, function httpBatchLink<TRouter extends AnyRouter>(opts: HTTPBatchLinkOptions<TRouter["_def"]["_config"]["$types"]>): TRPCLink<TRouter>httpBatchLink } from '@trpc/client';
import { function createTRPCOptionsProxy<TRouter extends AnyRouter, TFeatureFlags extends FeatureFlags = {
keyPrefix: false;
}>(opts: TRPCOptionsProxyOptions<TRouter, TFeatureFlags>): TRPCOptionsProxy<TRouter, TFeatureFlags>
createTRPCOptionsProxy } from '@trpc/tanstack-react-query';
import type { type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter } from '~/server/router';
// Only in a client-only app: with server rendering, every request would share it
export const const queryClient: QueryClientqueryClient = new new QueryClient(config?: QueryClientConfig): QueryClientQueryClient({
QueryClientConfig.defaultOptions?: DefaultOptions<Error> | undefineddefaultOptions: { DefaultOptions<Error>.queries?: OmitKeyof<QueryObserverOptions<unknown, Error, unknown, unknown, readonly unknown[], never>, "suspense" | "queryKey", "strictly"> | undefinedqueries: { staleTime?: StaleTimeFunction<unknown, Error, unknown, readonly unknown[]> | undefinedstaleTime: 30_000 } },
});
export const const trpcClient: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpcClient = createTRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>(opts: CreateTRPCClientOptions<...>): TRPCClient<...>
createTRPCClient<type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter>({
links: TRPCLink<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>[]
links: [httpBatchLink<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>(opts: HTTPBatchLinkOptions<...>): TRPCLink<...>
httpBatchLink({ url: string | URLurl: '/api/trpc' })],
});
export const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = createTRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>(opts: TRPCOptionsProxyOptions<...>): TRPCOptionsProxy<...>
createTRPCOptionsProxy({ TRPCOptionsProxyOptionsExternal<BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<{ viewer: QueryProcedure<{ input: void; output: { id: string; name: string; orgId: string; }; meta: object; errorShape: DefaultErrorShape; }>; org: BuiltRouter<...>; user: BuiltRouter<...>; post: BuiltRouter<...>; }>>>.client: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>> | TRPCUntypedClient<...>
client: const trpcClient: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpcClient, queryClient: QueryClient | (() => QueryClient)queryClient });import { const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense } from 'react';
import { function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments } from '~/components/comments';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from '~/components/post';
import { const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc } from '~/trpc';
import type { Route } from './+types/post';
export async function function clientLoader({ params }: Route.ClientLoaderArgs): Promise<{
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>
clientLoader({ params: {
id: string;
}
params }: Route.type Route.ClientLoaderArgs = ClientDataFunctionArgs<{
id: string;
}> & {
serverLoader: () => Promise<undefined>;
}
ClientLoaderArgs) {
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: params: {
id: string;
}
params.id: stringid, limit?: number | undefinedlimit: 20 });
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid: params: {
id: string;
}
params.id: stringid });
return { comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments, post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post };
}
export default function function PostPage({ loaderData }: Route.ComponentProps): JSX.ElementPostPage({ loaderData: {
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}
loaderData }: Route.type Route.ComponentProps = {
params: {
id: string;
};
matches: [{
id: "routes/post";
params: Record<string, string | undefined>;
pathname: string;
loaderData: {
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
};
handle: unknown;
}, ...({
...;
} | undefined)[]];
} & {
...;
}
ComponentProps) {
return (
<>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={loaderData: {
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}
loaderData.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post} />
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.Attributes.key?: React.Key | null | undefinedkey={loaderData: {
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}
loaderData.post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post.unstable_TRPCQueryHandle<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.input: {
id: string;
}
input.id: stringid} React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading comments…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments={loaderData: {
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}
loaderData.comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
</>
);
}In Data Mode, the same function is a route object's loader, and the component reads it with useLoaderData:
import { const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense } from 'react';
import { function createBrowserRouter(routes: RouteObject[], opts?: DOMRouterOpts): RoutercreateBrowserRouter, function useLoaderData<T = any>(): SerializeFrom<T>useLoaderData, type interface LoaderFunctionArgs<Context = Readonly<RouterContextProvider>>LoaderFunctionArgs } from 'react-router';
import { function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments } from '~/components/comments';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from '~/components/post';
import { const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc } from '~/trpc';
async function function postLoader({ params }: LoaderFunctionArgs): Promise<{
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>
postLoader({ params: Params<string>params }: interface LoaderFunctionArgs<Context = Readonly<RouterContextProvider>>LoaderFunctionArgs) {
const const id: stringid = params: Params<string>params.string | undefinedid!;
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: const id: stringid, limit?: number | undefinedlimit: 20 });
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid });
return { comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments, post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post };
}
function function PostPage(): JSX.ElementPostPage() {
const { const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments, const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post } = useLoaderData<({ params }: LoaderFunctionArgs) => Promise<{
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>>(): {
...;
}
useLoaderData<typeof function postLoader({ params }: LoaderFunctionArgs): Promise<{
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>
postLoader>();
return (
<>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post} />
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.Attributes.key?: React.Key | null | undefinedkey={const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post.unstable_TRPCQueryHandle<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.input: {
id: string;
}
input.id: stringid} React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading comments…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments={const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
</>
);
}
export const const router: Routerrouter = function createBrowserRouter(routes: RouteObject[], opts?: DOMRouterOpts): RoutercreateBrowserRouter([
{ path?: string | undefinedpath: '/posts/:id', loader?: boolean | LoaderFunction<Readonly<RouterContextProvider>> | undefinedloader: function postLoader({ params }: LoaderFunctionArgs): Promise<{
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>
postLoader, Component?: React.ComponentType<{}> | null | undefinedComponent: function PostPage(): JSX.ElementPostPage },
]);- There is nothing to hydrate: the loader starts queries in the same
QueryClientthe components read. unstable_$ensurewaits on the first visit only. Coming back shows the cached post at once and refetches it in the background if it's stale.- Key
Suspenseboundaries by the route's input. React Router renders the next page in a transition, and a transition waits for a boundary that's already showing content instead of showing its fallback. Without the key, navigating from one post to another waits for the comments.
Server rendering
In Framework Mode with server rendering, loader runs on the server, for the first page load and for every navigation after it. Create a query client and proxy per request in a root middleware. Loaders return dehydrate(queryClient) with their handles, and the route component wraps its children in TanStack Query's HydrationBoundary, as with any TanStack Query loader:
import type { class QueryClientQueryClient } from '@tanstack/react-query';
import { function createTRPCOptionsProxy<TRouter extends AnyRouter, TFeatureFlags extends FeatureFlags = {
keyPrefix: false;
}>(opts: TRPCOptionsProxyOptions<TRouter, TFeatureFlags>): TRPCOptionsProxy<TRouter, TFeatureFlags>
createTRPCOptionsProxy, type type TRPCOptionsProxy<TRouter extends AnyTRPCRouter, TFeatureFlags extends FeatureFlags = { keyPrefix: false; }> = DecoratedRouterRecord<TRouter["_def"]["_config"]["$types"], TRouter["_def"]["record"], TFeatureFlags> & DecorateRouterKeyable<TFeatureFlags> & DecorateHandleReaders<TFeatureFlags>TRPCOptionsProxy } from '@trpc/tanstack-react-query';
import { function createContext<T>(defaultValue?: T): RouterContext<T>createContext } from 'react-router';
import { const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>
appRouter, type type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter } from '~/server/router';
import { const makeQueryClient: () => QueryClientmakeQueryClient } from './query-client';
export const const trpcContext: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>
trpcContext = createContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>(defaultValue?: {
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
} | undefined): RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>
createContext<{
queryClient: QueryClientqueryClient: class QueryClientQueryClient;
trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpc: type TRPCOptionsProxy<TRouter extends AnyTRPCRouter, TFeatureFlags extends FeatureFlags = { keyPrefix: false; }> = DecoratedRouterRecord<TRouter["_def"]["_config"]["$types"], TRouter["_def"]["record"], TFeatureFlags> & DecorateRouterKeyable<TFeatureFlags> & DecorateHandleReaders<TFeatureFlags>TRPCOptionsProxy<type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter>;
}>();
export function function createServerTRPC(): {
queryClient: QueryClient;
trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>;
}
createServerTRPC() {
const const queryClient: QueryClientqueryClient = function makeQueryClient(): QueryClientmakeQueryClient();
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc = createTRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>(opts: TRPCOptionsProxyOptions<...>): TRPCOptionsProxy<...>
createTRPCOptionsProxy({ TRPCOptionsProxyOptionsInternal<BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<{ viewer: QueryProcedure<{ input: void; output: { id: string; name: string; orgId: string; }; meta: object; errorShape: DefaultErrorShape; }>; org: BuiltRouter<...>; user: BuiltRouter<...>; post: BuiltRouter<...>; }>>>.router: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>
router: const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>
appRouter, TRPCOptionsProxyOptionsInternal<BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<{ viewer: QueryProcedure<{ input: void; output: { id: string; name: string; orgId: string; }; meta: object; errorShape: DefaultErrorShape; }>; org: BuiltRouter<...>; user: BuiltRouter<...>; post: BuiltRouter<...>; }>>>.ctx: object | (() => MaybePromise<object>)ctx: {}, queryClient: QueryClient | (() => QueryClient)queryClient });
return { queryClient: QueryClientqueryClient, trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc };
}import { const QueryClientProvider: ({ client, children }: QueryClientProviderProps) => React.JSX.ElementQueryClientProvider } from '@tanstack/react-query';
import { function createTRPCClient<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter>): TRPCClient<TRouter>createTRPCClient } from '@trpc/client';
import { function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>] (+1 overload)useState } from 'react';
import { function Outlet(props: OutletProps): React.ReactElement | nullOutlet } from 'react-router';
import type { type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter } from '~/server/router';
import { const TRPCProvider: TRPCProviderType<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
TRPCProvider } from '~/trpc/client';
import type { Route } from './+types/root';
import { function makeQueryClient(): QueryClientmakeQueryClient } from './trpc/query-client';
import { function createServerTRPC(): {
queryClient: QueryClient;
trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>;
}
createServerTRPC, const trpcContext: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>
trpcContext } from './trpc/server';
export const const middleware: CreateServerMiddlewareFunction<Info & {
module: typeof import("./root");
matches: [{
id: "root";
module: typeof import("./root");
}];
}>[]
middleware: Route.type Route.MiddlewareFunction = (args: ServerDataFunctionArgs<{
[x: string]: string | undefined;
}>, next: MiddlewareNextFunction<Response>) => MaybePromise<void | Response>
MiddlewareFunction[] = [
({ context: Readonly<RouterContextProvider>context }) => {
context: Readonly<RouterContextProvider>context.set<RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>>(context: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>, value: {
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}): void
set(const trpcContext: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>
trpcContext, function createServerTRPC(): {
queryClient: QueryClient;
trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>;
}
createServerTRPC());
},
];
export default function function App(): JSX.ElementApp() {
const [const queryClient: QueryClientqueryClient] = useState<QueryClient>(initialState: QueryClient | (() => QueryClient)): [QueryClient, React.Dispatch<React.SetStateAction<QueryClient>>] (+1 overload)useState(function makeQueryClient(): QueryClientmakeQueryClient);
const [const trpcClient: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpcClient] = useState<TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>>(initialState: TRPCClient<...> | (() => TRPCClient<...>)): [...] (+1 overload)
useState(() => createTRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>(opts: CreateTRPCClientOptions<...>): TRPCClient<...>
createTRPCClient<type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>> & DecorateCreateRouterOptions<...>
AppRouter>({ links: TRPCLink<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>[]
links: [/* ... */] }));
return (
<const QueryClientProvider: ({ client, children }: QueryClientProviderProps) => React.JSX.ElementQueryClientProvider client: QueryClientclient={const queryClient: QueryClientqueryClient}>
<const TRPCProvider: TRPCProviderType<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
TRPCProvider trpcClient: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpcClient={const trpcClient: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpcClient} queryClient: QueryClientqueryClient={const queryClient: QueryClientqueryClient}>
<function Outlet(props: OutletProps): React.ReactElement | nullOutlet />
</const TRPCProvider: TRPCProviderType<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
TRPCProvider>
</const QueryClientProvider: ({ client, children }: QueryClientProviderProps) => React.JSX.ElementQueryClientProvider>
);
}import { function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedStatedehydrate, const HydrationBoundary: ({ children, options, state, queryClient }: HydrationBoundaryProps) => React.ReactElementHydrationBoundary } from '@tanstack/react-query';
import { const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense } from 'react';
import { function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments } from '~/components/comments';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from '~/components/post';
import { const trpcContext: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>
trpcContext } from '../trpc/server';
import type { Route } from './+types/post';
export async function function loader({ params, context }: Route.LoaderArgs): Promise<{
dehydratedState: DehydratedState;
comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>;
}>
loader({ params: {
id: string;
}
params, context: Readonly<RouterContextProvider>context }: Route.type Route.LoaderArgs = {
request: Request;
url: URL;
params: {
id: string;
};
pattern: string;
context: Readonly<RouterContextProvider>;
}
LoaderArgs) {
const { const queryClient: QueryClientqueryClient, const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpc } = context: Readonly<RouterContextProvider>context.get<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>(context: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>): {
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}
get(const trpcContext: RouterContext<{
queryClient: QueryClient;
trpc: TRPCOptionsProxy<AppRouter>;
}>
trpcContext);
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: params: {
id: string;
}
params.id: stringid, limit?: number | undefinedlimit: 20 });
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { ...; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid: params: {
id: string;
}
params.id: stringid });
return { dehydratedState: DehydratedStatedehydratedState: function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedStatedehydrate(const queryClient: QueryClientqueryClient), comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments, post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post };
}
export default function function PostPage({ loaderData }: Route.ComponentProps): JSX.ElementPostPage({ loaderData: {
dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
};
comments: {
...;
};
post: {
...;
};
}
loaderData }: Route.type Route.ComponentProps = {
params: {
id: string;
};
matches: [{
id: "routes/post";
params: Record<string, string | undefined>;
pathname: string;
loaderData: {
dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
};
comments: {
...;
};
post: {
...;
};
};
handle: unknown;
}, ...({
...;
} | undefined)[]];
} & {
...;
}
ComponentProps) {
return (
<const HydrationBoundary: ({ children, options, state, queryClient }: HydrationBoundaryProps) => React.ReactElementHydrationBoundary HydrationBoundaryProps.state: DehydratedState | null | undefinedstate={loaderData: {
dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
};
comments: {
...;
};
post: {
...;
};
}
loaderData.dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
}
dehydratedState}>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={loaderData: {
dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
};
comments: {
...;
};
post: {
...;
};
}
loaderData.post: {
readonly queryKey: [path: string[], opts?: {
input?: {
id: string;
} | undefined;
type?: "query" | undefined;
} | undefined];
readonly input: {
id: string;
};
readonly [handleDef]: {
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: {
message: string;
code: TRPC_ERROR_CODE_NUMBER;
data: DefaultErrorData;
};
featureFlags: {
keyPrefix: false;
};
kind: "query";
};
}
post} />
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.Attributes.key?: React.Key | null | undefinedkey={loaderData: {
dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
};
comments: {
...;
};
post: {
...;
};
}
loaderData.post: {
readonly queryKey: [path: string[], opts?: {
input?: {
id: string;
} | undefined;
type?: "query" | undefined;
} | undefined];
readonly input: {
id: string;
};
readonly [handleDef]: {
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: {
message: string;
code: TRPC_ERROR_CODE_NUMBER;
data: DefaultErrorData;
};
featureFlags: {
keyPrefix: false;
};
kind: "query";
};
}
post.input: {
id: string;
}
input.id: stringid} React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading comments…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments={loaderData: {
dehydratedState: {
mutations: {
mutationKey?: readonly undefined[] | undefined;
state: {
context: undefined;
data: undefined;
error: Error | null;
failureCount: number;
failureReason: Error | null;
isPaused: boolean;
status: MutationStatus;
variables: undefined;
submittedAt: number;
};
meta?: {
[x: string]: undefined;
} | undefined;
scope?: MutationScope | undefined;
}[];
queries: {
queryHash: string;
queryKey: readonly undefined[];
state: {
data: undefined;
dataUpdateCount: number;
dataUpdatedAt: number;
error: Error | null;
... 7 more ...;
fetchStatus: FetchStatus;
};
dehydratedAt: number;
promise?: Promise<...> | undefined;
meta?: {
[x: string]: undefined;
} | undefined;
queryType?: "infinite" | undefined;
}[];
};
comments: {
...;
};
post: {
...;
};
}
loaderData.comments: {
readonly queryKey: [path: string[], opts?: {
input?: Omit<{
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, "cursor" | "direction"> | undefined;
type?: "infinite" | undefined;
} | undefined];
readonly input: Omit<{
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, "cursor" | "direction">;
readonly initialCursor?: number | null | undefined;
readonly [handleDef]: {
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: {
message: string;
code: TRPC_ERROR_CODE_NUMBER;
data: DefaultErrorData;
};
featureFlags: {
keyPrefix: false;
};
kind: "infinite";
};
}
comments} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
</const HydrationBoundary: ({ children, options, state, queryClient }: HydrationBoundaryProps) => React.ReactElementHydrationBoundary>
);
}makeQueryClientis the query client factory from the server components guide: it dehydrates pending queries and runs your transformer'sserializeanddeserialize.- React Router streams promises in loader data, so the comments' pending query crosses as a promise inside
dehydratedState. The page's HTML doesn't wait for it, and the browser doesn't fetch it again. dehydrate(queryClient)sends everything the request's client holds. Nested routes share it, so a query another loader started can be sent twice. Filter withdehydrate'sshouldDehydrateQueryif that matters.- Nested routes share the request's proxy, since the middleware runs once per request. A query that two loaders start is requested once.
- In a
loader,unstable_$ensurewaits likeunstable_$fetch, since the request'sQueryClientstarts empty. Useunstable_$prefetchfor queries the page can stream.
Next.js App Router
This builds on the Next.js App Router setup. The query client factory, getQueryClient and the server proxy in trpc/server.tsx stay as they are. Add export type TRPC = TRPCOptionsProxy<AppRouter> to trpc/client.tsx, and in trpc/server.tsx, replace the setup's HydrateClient with one from unstable_createHydrateClient:
import { function unstable_createHydrateClient(_queryClient: QueryClient | (() => QueryClient)): React.FC<unstable_HydrateClientProps>unstable_createHydrateClient } from '@trpc/tanstack-react-query';
// ... `getQueryClient` and the `trpc` proxy as before
export const const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient = function unstable_createHydrateClient(_queryClient: QueryClient | (() => QueryClient)): React.FC<unstable_HydrateClientProps>unstable_createHydrateClient(const getQueryClient: () => QueryClientgetQueryClient);The client components are the same either way: they read the handle with useSuspenseQuery or useQuery, as in Typing props.
HydrateClient
HydrateClient sends the queries the request has started so far to the browser, including pending ones, which stream in as they settle. Render one in each server component that starts queries for client components:
import { const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense } from 'react';
import { const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient, const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc } from '~/trpc/server';
import { function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments } from './comments';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from './post';
export default async function function PostPage(props: {
params: Promise<{
id: string;
}>;
}): Promise<JSX.Element>
PostPage(props: {
params: Promise<{
id: string;
}>;
}
props: { params: Promise<{
id: string;
}>
params: interface Promise<T>Promise<{ id: stringid: string }> }) {
const { const id: stringid } = await props: {
params: Promise<{
id: string;
}>;
}
props.params: Promise<{
id: string;
}>
params;
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid });
return (
<const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient>
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading post…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading comments…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function CommentsSection(props: {
id: string;
}): Promise<JSX.Element>
CommentsSection id: stringid={const id: stringid} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
</const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient>
);
}
async function function CommentsSection(props: {
id: string;
}): Promise<JSX.Element>
CommentsSection(props: {
id: string;
}
props: { id: stringid: string }) {
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: props: {
id: string;
}
props.id: stringid, limit?: number | undefinedlimit: 20 });
return (
<const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient>
<function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments={const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments} />
</const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient>
);
}- A query an earlier
HydrateClientsent in the same request isn't sent again, so nesting them is fine. The outer one renders beforeCommentsSectionstarts its query, so each sends only its own. - A query is sent again when the server has newer data for it, for example after
unstable_$fetchrefetched it. - It keeps your query client's
dehydrateoptions:shouldDehydrateQuery,serializeDataand the rest. - It works only in server components.
@trpc/tanstack-react-queryresolves to its server build through thereact-serverexport condition, which Next.js and other RSC bundlers set for server components. Rendered anywhere else, it throws. - It works for queries started with
queryClient.prefetchQuery(trpc.x.queryOptions(input))too, not only handles.
Streaming with unstable_$prefetch
The page sends its HTML at once. Each Suspense boundary shows its fallback, then the data streams in:
import { const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense } from 'react';
import { const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient, const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc } from '~/trpc/server';
import { function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments } from './comments';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from './post';
export default async function function PostPage(props: {
params: Promise<{
id: string;
}>;
}): Promise<JSX.Element>
PostPage(props: {
params: Promise<{
id: string;
}>;
}
props: {
params: Promise<{
id: string;
}>
params: interface Promise<T>Promise<{ id: stringid: string }>;
}) {
const { const id: stringid } = await props: {
params: Promise<{
id: string;
}>;
}
props.params: Promise<{
id: string;
}>
params;
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetch: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetch({ id: stringid });
const const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments = const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.comments: DecorateQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & DecorateInfiniteQueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments.DecorateInfiniteQueryHandleVerbs<{ input: { postId: string; limit?: number | undefined; cursor?: number | null | undefined; }; output: { items: { id: string; text: string; }[]; nextCursor: number | null; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$prefetchInfinite: (input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
}, opts?: unstable_TRPCPrefetchInfiniteOptions<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> | undefined) => unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
unstable_$prefetchInfinite({ postId: stringpostId: const id: stringid, limit?: number | undefinedlimit: 20 });
return (
<const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient>
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading post…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
<const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense React.SuspenseProps.fallback?: React.ReactNodefallback={<React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Loading comments…</React.JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>}>
<function Comments(props: {
comments: unstable_inferInfiniteQueryHandle<TRPC["post"]["comments"]>;
}): JSX.Element
Comments comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments={const comments: unstable_TRPCInfiniteQueryHandle<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
comments} />
</const Suspense: React.ExoticComponent<React.SuspenseProps>Suspense>
</const HydrateClient: React.FC<unstable_HydrateClientProps>HydrateClient>
);
}- Give each handle its own
Suspenseboundary, so one slow query doesn't hold back the rest. - If the post doesn't exist, the
Postcomponent throws, and the nearesterror.tsxshows the error.
Waiting with unstable_$ensure
The page waits for the post before it renders. Use this when the page can't render without the data, or when you need it on the server, for example to call notFound():
import { class TRPCErrorTRPCError } from '@trpc/server';
import { function notFound(): nevernotFound } from 'next/navigation';
import { const HydrateClient: FC<unstable_HydrateClientProps>HydrateClient, const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc } from '~/trpc/server';
import { function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post } from './post';
export default async function function PostPage(props: {
params: Promise<{
id: string;
}>;
}): Promise<JSX.Element>
PostPage(props: {
params: Promise<{
id: string;
}>;
}
props: {
params: Promise<{
id: string;
}>
params: interface Promise<T>Promise<{ id: stringid: string }>;
}) {
const { const id: stringid } = await props: {
params: Promise<{
id: string;
}>;
}
props.params: Promise<{
id: string;
}>
params;
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
viewer: QueryProcedure<{
input: void;
output: {
id: string;
name: string;
orgId: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
org: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
orgId: string;
};
output: {
id: string;
name: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
user: BuiltRouter<...>;
post: BuiltRouter<...>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: Post;
meta: object;
errorShape: DefaultErrorShape;
}>;
related: QueryProcedure<{
input: {
id: string;
};
output: Post[];
meta: object;
errorShape: DefaultErrorShape;
}>;
comments: QueryProcedure<{
input: {
postId: string;
limit?: number | undefined;
cursor?: number | null | undefined;
};
output: {
items: {
id: string;
text: string;
}[];
nextCursor: number | null;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid }).Promise<unstable_TRPCQueryHandle<{ input: { id: string; }; output: Post; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>>.catch<never>(onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
catch((cause: anycause) => {
if (cause: anycause instanceof class TRPCErrorTRPCError && cause: TRPCErrorcause.TRPCError.code: "BAD_GATEWAY" | "BAD_REQUEST" | "CLIENT_CLOSED_REQUEST" | "CONFLICT" | "FORBIDDEN" | "GATEWAY_TIMEOUT" | "INTERNAL_SERVER_ERROR" | "METHOD_NOT_SUPPORTED" | "NOT_FOUND" | "NOT_IMPLEMENTED" | "PARSE_ERROR" | "PAYLOAD_TOO_LARGE" | "PAYMENT_REQUIRED" | "PRECONDITION_FAILED" | "PRECONDITION_REQUIRED" | "SERVICE_UNAVAILABLE" | "TIMEOUT" | "TOO_MANY_REQUESTS" | "UNAUTHORIZED" | "UNPROCESSABLE_CONTENT" | "UNSUPPORTED_MEDIA_TYPE"code === 'NOT_FOUND') function notFound(): nevernotFound();
throw cause: anycause;
});
return (
<const HydrateClient: FC<unstable_HydrateClientProps>HydrateClient>
<function Post(props: {
post: unstable_inferQueryHandle<TRPC["post"]["byId"]>;
}): JSX.Element
Post post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post={const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: Post;
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post} />
</const HydrateClient: FC<unstable_HydrateClientProps>HydrateClient>
);
}- The data is in the cache before
Postrenders, so it shows the post at once. If the cache loses the post later,Postsuspends and shows the nearestloading.tsxorSuspensefallback: see When the cache loses the data. - To use the data on the server, read it from the cache: see Using the data in a loader.
To wait for some queries and stream others on the same page, see Combining both.
Handle inputs
Handle inputs cross to the client inside the handle, through React's own serialization, not your tRPC data transformer. This isn't new: dehydrated query keys already cross the same way. Keep inputs to plain JSON values and Dates. React can send a Map, Set or BigInt too, but TanStack Query hashes query keys with JSON.stringify: a BigInt input throws, and two different Map or Set inputs share one cache entry.
Errors
With unstable_$prefetch
unstable_$prefetch never throws. The component that reads the handle gets the error instead, and the nearest error boundary shows it:
- If the query failed before it was dehydrated, it isn't sent. The browser fetches it again and gets a
TRPCClientError. - If it was dehydrated while pending and failed later, TanStack Query passes a redacted error to the browser. A handle read then refetches the query once, even with
retry: false, so error boundaries get aTRPCClientErrorwithdata.code. A query that always fails costs one extra request.
To log or report these errors in one place, whichever component reads the handle, use the onError callback of your QueryClient's QueryCache.
With unstable_$ensure
unstable_$ensure and unstable_$fetch reject where you await them:
- On the server proxy, with a
TRPCError. - On a client-backed proxy, such as one in a TanStack Start loader, with a
TRPCClientError.
Invalidation and staleness
Handle reads use normal tRPC query keys, so invalidation doesn't change: queryClient.invalidateQueries(trpc.post.pathFilter()), trpc.post.byId.queryFilter(input) and { queryKey: handle.queryKey } all match.
unstable_$fromHandle keeps TanStack Query's refetch defaults. Hydrated data keeps the server's fetch time, so set a staleTime above 0 in your query client, as the setup guides do, or the client refetches right after hydration. To change it for one read, pass staleTime to unstable_$fromHandle.
What a handle guarantees
- Types: only
unstable_$prefetch,unstable_$ensureandunstable_$fetchcreate handles. The key, procedure, input and kind (query or infinite) always agree, and output,selectand error types flow to the read. - Runtime:
unstable_$fromHandlechecks the handle's shape andkeyPrefix. - Development: if a handle's query runs during server rendering, its data never reached that
QueryClient. tRPC logs a warning once per query. The warning is off in production.
Handles aren't signed. A handle names a query the browser could call anyway, and every refetch runs input validation and auth on the server again. Put permission checks in your procedures.
Testing components that take handles
Handles can't be written by hand, so create them in the test the way a loader does: with a proxy over a test QueryClient. Seed the cache with fixture data, then unstable_$ensure returns a handle without calling the procedure:
import { class QueryClientQueryClient } from '@tanstack/react-query';
import { function createTRPCOptionsProxy<TRouter extends AnyRouter, TFeatureFlags extends FeatureFlags = {
keyPrefix: false;
}>(opts: TRPCOptionsProxyOptions<TRouter, TFeatureFlags>): TRPCOptionsProxy<TRouter, TFeatureFlags>
createTRPCOptionsProxy } from '@trpc/tanstack-react-query';
import { const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>
appRouter } from './router';
const const queryClient: QueryClientqueryClient = new new QueryClient(config?: QueryClientConfig): QueryClientQueryClient({
QueryClientConfig.defaultOptions?: DefaultOptions<Error> | undefineddefaultOptions: { DefaultOptions<Error>.queries?: OmitKeyof<QueryObserverOptions<unknown, Error, unknown, unknown, readonly unknown[], never>, "suspense" | "queryKey", "strictly"> | undefinedqueries: { staleTime?: StaleTimeFunction<unknown, Error, unknown, readonly unknown[]> | undefinedstaleTime: var Infinity: numberInfinity, retry?: RetryValue<Error> | undefinedretry: false } },
});
const const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc = createTRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>(opts: TRPCOptionsProxyOptions<...>): TRPCOptionsProxy<...>
createTRPCOptionsProxy({ TRPCOptionsProxyOptionsInternal<BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<{ post: BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<...>>; }>>>.router: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>
router: const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>
appRouter, TRPCOptionsProxyOptionsInternal<BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<{ post: BuiltRouter<{ ctx: object; meta: object; errorShape: DefaultErrorShape; transformer: false; }, DecorateCreateRouterOptions<...>>; }>>>.ctx: object | (() => MaybePromise<object>)ctx: {}, queryClient: QueryClient | (() => QueryClient)queryClient });
const queryClient: QueryClientqueryClient.QueryClient.setQueryData<unknown, TRPCQueryKeyWithoutPrefix & {
[dataTagSymbol]: {
id: string;
title: string;
};
[dataTagErrorSymbol]: TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>;
}, {
id: string;
title: string;
}>(queryKey: TRPCQueryKeyWithoutPrefix & {
[dataTagSymbol]: {
id: string;
title: string;
};
[dataTagErrorSymbol]: TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>;
}, updater: Updater<...>, options?: SetDataOptions): NoInfer<{
id: string;
title: string;
}> | undefined
setQueryData(const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryProcedure<{ input: { id: string; }; output: { id: string; title: string; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.queryKey: (input?: Partial<{
id: string;
}> | undefined) => TRPCQueryKeyWithoutPrefix & {
[dataTagSymbol]: {
id: string;
title: string;
};
[dataTagErrorSymbol]: TRPCClientErrorLike<{
transformer: false;
errorShape: DefaultErrorShape;
}>;
}
queryKey({ id?: string | undefinedid: '1' }), {
id: stringid: '1',
title: stringtitle: 'Hello',
});
const const post: unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>
post = await const trpc: TRPCOptionsProxy<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
post: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>>;
}>>, {
...;
}>
trpc.post: DecoratedRouterRecord<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{
byId: QueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
meta: object;
errorShape: DefaultErrorShape;
}>;
}>, {
...;
}> & DecorateRouterKeyable<...> & DecorateHandleReaders<...>
post.byId: DecorateQueryProcedure<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}> & Record<string, never>
byId.DecorateQueryHandleVerbs<{ input: { id: string; }; output: { id: string; title: string; }; transformer: false; errorShape: DefaultErrorShape; featureFlags: { keyPrefix: false; }; }>.unstable_$ensure: (input: {
id: string;
}, opts?: unstable_TRPCPrefetchOptions | undefined) => Promise<unstable_TRPCQueryHandle<{
input: {
id: string;
};
output: {
id: string;
title: string;
};
transformer: false;
errorShape: DefaultErrorShape;
featureFlags: {
keyPrefix: false;
};
}>>
unstable_$ensure({ id: stringid: '1' });- Render the component under a
QueryClientProviderandTRPCProviderwith the samequeryClient. The component reads the fixture from the cache. - Leave out
setQueryDatato run the real procedure, or use a client withlocalLinkinstead ofrouterto test through your links. unstable_$prefetchgives a handle for a loading state: with a procedure that never resolves, the reader suspends.
Where handles don't work
- Server functions and server actions: they run with a
QueryClientthat never hydrates the page. Return data from them, not handles. - URLs and storage: a handle in the URL or
localStoragegains nothing over the input itself. - Event handlers: to preload on hover, use
queryClient.prefetchQuery(trpc.x.queryOptions(input)). In render, useunstable_$prefetchonuseTRPC().
FAQ
Why do some methods have a $?
Methods with a $, such as unstable_$prefetch and unstable_$fromHandle, are tRPC helpers built on TanStack Query. Anything without a $, such as queryOptions, queryKey and pathFilter, returns plain TanStack Query options, keys or filters. Each $ method is one TanStack Query call: see TanStack Query equivalents.