跳至主要内容
版本:11.x

中止过程调用

默认情况下,tRPC 不会在卸载时取消请求。如果您希望选择此行为,可以在配置回调中提供 abortOnUnmount

全局

client.ts
ts
// @filename: utils.ts
import { createTRPCNext } from '@trpc/next';
 
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
// ...
abortOnUnmount: true,
};
},
});
client.ts
ts
// @filename: utils.ts
import { createTRPCNext } from '@trpc/next';
 
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
// ...
abortOnUnmount: true,
};
},
});

按请求

您还可以在请求级别覆盖此行为。

client.ts
ts
// @filename: pages/posts/[id].tsx
import { trpc } from '~/utils/trpc';
 
const PostViewPage: NextPageWithLayout = () => {
const id = useRouter().query.id as string;
const postQuery = trpc.post.byId.useQuery({ id }, { trpc: { abortOnUnmount: true } });
 
return (...)
}
client.ts
ts
// @filename: pages/posts/[id].tsx
import { trpc } from '~/utils/trpc';
 
const PostViewPage: NextPageWithLayout = () => {
const id = useRouter().query.id as string;
const postQuery = trpc.post.byId.useQuery({ id }, { trpc: { abortOnUnmount: true } });
 
return (...)
}