Saltar al contenido principal
Versión: 10.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
// @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,
};
},
});

Por solicitud

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

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 (...)
}