Saltar al contenido principal
Versión: 11.x

Cancelación de llamadas a procedimientos

Traducción Beta No Oficial

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

Por defecto, tRPC no cancela las solicitudes al desmontar componentes. Si deseas habilitar este comportamiento, puedes incluir abortOnUnmount en tu callback de configuración.

Globalmente

client.ts
ts
import { httpBatchLink } from '@trpc/client';
import { createTRPCNext } from '@trpc/next';
import type { AppRouter } from './server/routers/_app';
 
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
links: [
httpBatchLink({
url: '/api/trpc',
}),
],
abortOnUnmount: true,
};
},
});
client.ts
ts
import { httpBatchLink } from '@trpc/client';
import { createTRPCNext } from '@trpc/next';
import type { AppRouter } from './server/routers/_app';
 
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
links: [
httpBatchLink({
url: '/api/trpc',
}),
],
abortOnUnmount: true,
};
},
});

Por solicitud

También puedes sobrescribir este comportamiento a nivel de solicitud individual.

client.ts
tsx
import { trpc } from './utils/trpc';
import { useRouter } from 'next/router';
 
function PostViewPage() {
const id = useRouter().query.id as string;
const postQuery = trpc.post.byId.useQuery({ id }, { trpc: { abortOnUnmount: true } });
 
return null;
}
client.ts
tsx
import { trpc } from './utils/trpc';
import { useRouter } from 'next/router';
 
function PostViewPage() {
const id = useRouter().query.id as string;
const postQuery = trpc.post.byId.useQuery({ id }, { trpc: { abortOnUnmount: true } });
 
return null;
}