useSubscription()
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
useSubscription 钩子可用于订阅服务器上的订阅过程。
函数签名
选项
技巧
- 如果需要设置选项但不想传递输入值,可以传递
undefined - 如果传递来自
@tanstack/react-query的skipToken,订阅将被暂停 - 查看我们的 SSE 示例了解完整用法
tsxinterfaceUseTRPCSubscriptionOptions <TOutput ,TError > {/*** Called when the subscription is started.*/onStarted ?: () => void;/*** Called when new data is received from the subscription.*/onData ?: (data :TOutput ) => void;/*** Called when an **unrecoverable error** occurs and the subscription is stopped.*/onError ?: (error :TError ) => void;/*** Called when the subscription is completed on the server.* The state will transition to `'idle'` with `data: undefined`.*/onComplete ?: () => void;/*** @deprecated Use a `skipToken` from `@tanstack/react-query` instead.* This will be removed in v12.*/enabled ?: boolean;}
tsxinterfaceUseTRPCSubscriptionOptions <TOutput ,TError > {/*** Called when the subscription is started.*/onStarted ?: () => void;/*** Called when new data is received from the subscription.*/onData ?: (data :TOutput ) => void;/*** Called when an **unrecoverable error** occurs and the subscription is stopped.*/onError ?: (error :TError ) => void;/*** Called when the subscription is completed on the server.* The state will transition to `'idle'` with `data: undefined`.*/onComplete ?: () => void;/*** @deprecated Use a `skipToken` from `@tanstack/react-query` instead.* This will be removed in v12.*/enabled ?: boolean;}
返回类型
返回类型是基于 status 的可辨识联合类型:
tstypeTRPCSubscriptionResult <TOutput ,TError > =|TRPCSubscriptionIdleResult <TOutput >|TRPCSubscriptionConnectingResult <TOutput ,TError >|TRPCSubscriptionPendingResult <TOutput >|TRPCSubscriptionErrorResult <TOutput ,TError >;interfaceTRPCSubscriptionIdleResult <TOutput > {/** Subscription is disabled or has ended */status : 'idle';data : undefined;error : null;reset : () => void;}interfaceTRPCSubscriptionConnectingResult <TOutput ,TError > {/** Trying to establish a connection (may have a previous error from a reconnection attempt) */status : 'connecting';data :TOutput | undefined;error :TError | null;reset : () => void;}interfaceTRPCSubscriptionPendingResult <TOutput > {/** Connected to the server, receiving data */status : 'pending';data :TOutput | undefined;error : null;reset : () => void;}interfaceTRPCSubscriptionErrorResult <TOutput ,TError > {/** An unrecoverable error occurred and the subscription is stopped */status : 'error';data :TOutput | undefined;error :TError ;reset : () => void;}
tstypeTRPCSubscriptionResult <TOutput ,TError > =|TRPCSubscriptionIdleResult <TOutput >|TRPCSubscriptionConnectingResult <TOutput ,TError >|TRPCSubscriptionPendingResult <TOutput >|TRPCSubscriptionErrorResult <TOutput ,TError >;interfaceTRPCSubscriptionIdleResult <TOutput > {/** Subscription is disabled or has ended */status : 'idle';data : undefined;error : null;reset : () => void;}interfaceTRPCSubscriptionConnectingResult <TOutput ,TError > {/** Trying to establish a connection (may have a previous error from a reconnection attempt) */status : 'connecting';data :TOutput | undefined;error :TError | null;reset : () => void;}interfaceTRPCSubscriptionPendingResult <TOutput > {/** Connected to the server, receiving data */status : 'pending';data :TOutput | undefined;error : null;reset : () => void;}interfaceTRPCSubscriptionErrorResult <TOutput ,TError > {/** An unrecoverable error occurred and the subscription is stopped */status : 'error';data :TOutput | undefined;error :TError ;reset : () => void;}
示例过程
server/routers/_app.tstsximportEventEmitter , {on } from 'events';import {initTRPC } from '@trpc/server';export constt =initTRPC .create ();typePost = {id : string;title : string };constee = newEventEmitter ();export constappRouter =t .router ({onPostAdd :t .procedure .subscription (async function* (opts ) {for await (const [data ] ofon (ee , 'add', {signal :opts .signal ,})) {constpost =data asPost ;yieldpost ;}}),});export typeAppRouter = typeofappRouter ;
server/routers/_app.tstsximportEventEmitter , {on } from 'events';import {initTRPC } from '@trpc/server';export constt =initTRPC .create ();typePost = {id : string;title : string };constee = newEventEmitter ();export constappRouter =t .router ({onPostAdd :t .procedure .subscription (async function* (opts ) {for await (const [data ] ofon (ee , 'add', {signal :opts .signal ,})) {constpost =data asPost ;yieldpost ;}}),});export typeAppRouter = typeofappRouter ;
示例 React 组件
components/PostFeed.tsxtsximport {trpc } from '../utils/trpc';typePost = {id : string;title : string };export functionPostFeed () {const [posts ,setPosts ] =React .useState <Post []>([]);constsubscription =trpc .onPostAdd .useSubscription (undefined , {onData : (post ) => {setPosts ((prev ) => [...prev ,post ]);},});return (<div ><h1 >Live Feed</h1 >{subscription .status === 'connecting' && <p >Connecting...</p >}{subscription .status === 'error' && (<div ><p >Error: {subscription .error .message }</p ><button onClick ={() =>subscription .reset ()}>Reconnect</button ></div >)}<ul >{posts .map ((post ) => (<li key ={post .id }>{post .title }</li >))}</ul ></div >);}
components/PostFeed.tsxtsximport {trpc } from '../utils/trpc';typePost = {id : string;title : string };export functionPostFeed () {const [posts ,setPosts ] =React .useState <Post []>([]);constsubscription =trpc .onPostAdd .useSubscription (undefined , {onData : (post ) => {setPosts ((prev ) => [...prev ,post ]);},});return (<div ><h1 >Live Feed</h1 >{subscription .status === 'connecting' && <p >Connecting...</p >}{subscription .status === 'error' && (<div ><p >Error: {subscription .error .message }</p ><button onClick ={() =>subscription .reset ()}>Reconnect</button ></div >)}<ul >{posts .map ((post ) => (<li key ={post .id }>{post .title }</li >))}</ul ></div >);}