Capítulo 39 de 57
TanStack Router deliberately does not manage mutation/submission state itself; its role is limited to invalidating loader data and reacting to URL side effects after a mutation, so mutation state should be owned by a dedicated library (TanStack Query, SWR, RTK Query, Redux, etc.) and coordinated with the router via router.invalidate() and router.subscribe().
router.invalidate(): invalidates committed, cached, and in-flight loader generations matching the current state; retires matching active preload lanes and reloads current active matches through the normal loading protocol. By default this is non-blocking (stale data revalidates in the background).router.invalidate({ sync: true }): awaits the invalidation until all affected loaders have finished reloading, useful when you need to guarantee fresh data before proceeding (e.g. before a redirect).key: ['sendMessage', roomId]), so mutation state automatically resets when the key (like a route param) changes.router.subscribe('onResolved', callback): fallback solution for libraries without a keying mechanism; the onResolved event fires when the location path changes (not just reloads) and has fully resolved, a good place to manually clear stale mutation caches.const router = useRouter()
const addTodo = async (todo: Todo) => {
try {
await api.addTodo()
await router.invalidate({ sync: true })
} catch {
// handle error
}
}
const router = createRouter()
const coolMutationCache = createCoolMutationCache()
const unsubscribeFn = router.subscribe('onResolved', () => {
coolMutationCache.clear()
})
router.invalidate() after a mutation that affects loader data; use { sync: true } when you need to wait for the reload before continuing.router.subscribe cleanup when available, it's simpler and less error-prone.router.invalidate() operates on.