getQueryKey
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
routerやprocedureを受け入れるgetQueryKeyヘルパーを提供しています。これにより、ネイティブ関数に正しいクエリキーを簡単に渡せます。
tsx// Queriesdeclare functiongetQueryKey (procedure :AnyQueryProcedure ,input ?:DeepPartial <TInput >,type ?:QueryType , /** @default 'any' */):TRPCQueryKey ;// Routersdeclare functiongetQueryKey (router :AnyRouter ,):TRPCQueryKey ;typeQueryType = "query" | "infinite" | "any";// for useQuery ──┘ │ │// for useInfiniteQuery ────┘ │// will match all ───────────────────────┘
tsx// Queriesdeclare functiongetQueryKey (procedure :AnyQueryProcedure ,input ?:DeepPartial <TInput >,type ?:QueryType , /** @default 'any' */):TRPCQueryKey ;// Routersdeclare functiongetQueryKey (router :AnyRouter ,):TRPCQueryKey ;typeQueryType = "query" | "infinite" | "any";// for useQuery ──┘ │ │// for useInfiniteQuery ────┘ │// will match all ───────────────────────┘
注記
クエリタイプanyがキャッシュ内の全クエリにマッチするのは、使用されるreact queryメソッドがファジーマッチングをサポートしている場合のみです。詳細はTanStack/query#5111 (コメント)を参照してください。
tsximport {useIsFetching ,useQueryClient } from '@tanstack/react-query';import {getQueryKey } from '@trpc/react-query';import {trpc } from './utils/trpc';functionMyComponent () {constqueryClient =useQueryClient ();constposts =trpc .post .list .useQuery ();// See if a query is fetchingconstpostListKey =getQueryKey (trpc .post .list ,undefined , 'query');constisFetching =useIsFetching ({queryKey :postListKey });// Set some query defaults for an entire routerconstpostKey =getQueryKey (trpc .post );queryClient .setQueryDefaults (postKey , {staleTime : 30 * 60 * 1000 });// ...}
tsximport {useIsFetching ,useQueryClient } from '@tanstack/react-query';import {getQueryKey } from '@trpc/react-query';import {trpc } from './utils/trpc';functionMyComponent () {constqueryClient =useQueryClient ();constposts =trpc .post .list .useQuery ();// See if a query is fetchingconstpostListKey =getQueryKey (trpc .post .list ,undefined , 'query');constisFetching =useIsFetching ({queryKey :postListKey });// Set some query defaults for an entire routerconstpostKey =getQueryKey (trpc .post );queryClient .setQueryDefaults (postKey , {staleTime : 30 * 60 * 1000 });// ...}
ミューテーション
クエリと同様に、ミューテーション用のgetMutationKeyも提供しています。内部的にはgetQueryKeyと同じ関数ですが(技術的にはgetQueryKeyをミューテーションにも使用可能)、意味的な違いがあります。
tsximport { getMutationKey } from '@trpc/react-query';const mutationKey = getMutationKey(trpc.user.create);
tsximport { getMutationKey } from '@trpc/react-query';const mutationKey = getMutationKey(trpc.user.create);