useQuery()
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
注記
@trpc/react-queryが提供するフックは、@tanstack/react-queryの薄いラッパーです。オプションや使用方法の詳細については、クエリに関する公式ドキュメントを参照してください。
tsxfunction useQuery(input: TInput,opts?: UseTRPCQueryOptions;)interface UseTRPCQueryOptionsextends UseQueryOptions {trpc: {ssr?: boolean;abortOnUnmount?: boolean;context?: Record<string, unknown>;}}
tsxfunction useQuery(input: TInput,opts?: UseTRPCQueryOptions;)interface UseTRPCQueryOptionsextends UseQueryOptions {trpc: {ssr?: boolean;abortOnUnmount?: boolean;context?: Record<string, unknown>;}}
UseTRPCQueryOptionsは@tanstack/react-queryのUseQueryOptionsを拡張しているため、enabledやrefetchOnWindowFocusなど任意のオプションを利用できます。さらに、プロシージャレベルで特定の動作を有効/無効にするtrpc固有のオプションも提供されています:
-
trpc.ssr: グローバル設定でssr: trueにしている場合、この特定のクエリでSSRを無効化するにはfalseを設定します。逆方向の動作はサポートされていないことに注意してください。つまり、グローバル設定がfalseの場合にプロシージャレベルでSSRを有効化することはできません。 -
trpc.abortOnUnmount: グローバル設定を上書きし、アンマウント時のクエリ中断を有効/無効にします。 -
trpc.context: リンクで使用できる追加メタデータを付与します。
ヒント
オプションを設定したいが入力値を渡したくない場合、代わりにundefinedを渡せます。
バックエンドで設定したinputスキーマに基づき、inputに対するオートコンプリートが機能することに気づくでしょう。
例
Backend code
server/routers/_app.tstsximport { initTRPC } from '@trpc/server';import { z } from 'zod';export const t = initTRPC.create();export const appRouter = t.router({// Create procedure at path 'hello'hello: t.procedure// using zod schema to validate and infer input values.input(z.object({text: z.string().nullish(),}).nullish(),).query((opts) => {return {greeting: `hello ${opts.input?.text ?? 'world'}`,};}),});
server/routers/_app.tstsximport { initTRPC } from '@trpc/server';import { z } from 'zod';export const t = initTRPC.create();export const appRouter = t.router({// Create procedure at path 'hello'hello: t.procedure// using zod schema to validate and infer input values.input(z.object({text: z.string().nullish(),}).nullish(),).query((opts) => {return {greeting: `hello ${opts.input?.text ?? 'world'}`,};}),});
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {// input is optional, so we don't have to pass second argumentconst helloNoArgs = trpc.hello.useQuery();const helloWithArgs = trpc.hello.useQuery({ text: 'client' });return (<div><h1>Hello World Example</h1><ul><li>helloNoArgs ({helloNoArgs.status}):{' '}<pre>{JSON.stringify(helloNoArgs.data, null, 2)}</pre></li><li>helloWithArgs ({helloWithArgs.status}):{' '}<pre>{JSON.stringify(helloWithArgs.data, null, 2)}</pre></li></ul></div>);}
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {// input is optional, so we don't have to pass second argumentconst helloNoArgs = trpc.hello.useQuery();const helloWithArgs = trpc.hello.useQuery({ text: 'client' });return (<div><h1>Hello World Example</h1><ul><li>helloNoArgs ({helloNoArgs.status}):{' '}<pre>{JSON.stringify(helloNoArgs.data, null, 2)}</pre></li><li>helloWithArgs ({helloWithArgs.status}):{' '}<pre>{JSON.stringify(helloWithArgs.data, null, 2)}</pre></li></ul></div>);}