Typed Navigation with useNavigate and Link
What you'll learn: How to navigate between routes with full TypeScript autocomplete for paths, parameters, and search state in TanStack Router.
The Power of Type-Safe Navigation
In traditional routing libraries, navigating to /users/$userId means typing strings by hand—typos slip through until runtime. TanStack Router's useNavigate hook and Link component know your entire route tree's structure, giving you autocomplete for paths, compile-time errors for invalid routes, and type-checked parameters.
useNavigate: Programmatic Navigation
The useNavigate hook returns a function that takes a strongly-typed route definition:
const navigate = useNavigate();
// TypeScript knows all valid route paths
navigate({
to: '/users/$userId',
params: { userId: '123' }, // Required, type-checked
search: { tab: 'profile' } // Validated against route schema
});
If you try to: '/typo' or forget userId, TypeScript errors before you run the code. The search object is validated against the schema you defined when creating the route.
Link: Declarative Navigation
The Link component works identically but declaratively:
<Link
to="/users/$userId"
params={{ userId: user.id }}
search={{ tab: 'settings' }}
>
View Profile
</Link>
Again, all properties are type-checked. You can't accidentally pass the wrong param name or an invalid search value.
The Analogy
Think of it like a GPS that only shows real destinations. Traditional routers let you type any address (valid or not), while TanStack Router's navigation is like a dropdown menu—it only presents actual, reachable routes with the exact information they need.
Key Takeaway: TanStack Router's useNavigate and Link provide end-to-end type safety for navigation, catching invalid paths, missing parameters, and incorrect search state at compile time instead of runtime.