Navigation
Navigation in TanStack Router is fully type-safe and provides multiple ways to move between routes - from imperative navigation with hooks to declarative navigation with components.Why Navigation Matters
Navigation is how users move through your application. TanStack Router provides:- Type-safe navigation - Compile-time checking of paths and parameters
- Multiple navigation methods - Programmatic, declarative, and browser-based
- Relative navigation - Navigate relative to the current route
- Search param management - Update search params without full navigation
- Navigation lifecycle - Hooks for before/after navigation events
Link Component
TheLink component is the primary way to create navigation links.
Basic Links
Links with Parameters
Links with Search Parameters
validateSearch schema.
Updating Search Params
Update search params while staying on the current route:search prop accepts a function that receives previous search params.
Hash Navigation
Navigate to page anchors:State Passing
Pass state through navigation:useLocation().state.
Relative Navigation
Navigate relative to the current route path.Relative Link Paths
From Parameter
Specify the starting route for navigation:Active Link Styling
Style links based on active state:Using activeProps
Using activeOptions
Custom Active Styling
isActive and isTransitioning booleans.
Programmatic Navigation
Navigate imperatively using theuseNavigate hook.
Basic Navigation
Navigation with Parameters
useNavigate hook returns a function defined in packages/router-core/src/useNavigate.ts:4-13:
Replace Navigation
Replace history instead of pushing:Relative Navigation
Router Methods
The router instance provides navigation methods:Navigation History
Navigation Options
Fine-tune navigation behavior with options.View Transitions
Use the View Transitions API:Reset Scroll
Control scroll behavior:Hash Scroll Behavior
Customize scroll-to-hash:ScrollIntoViewOptions.
Navigation Events
React to navigation changes:Redirects
Perform server-side or client-side redirects.Throwing Redirects
beforeLoad or loader cancel the navigation.
Route-Based Redirects
Relative Redirects
Use the route’sredirect helper:
External Links
For external URLs, use standard anchor tags:Link component is for internal routing only.
Preloading
Preload routes before navigation:Best Practices
Prefer Link components over navigate()
Prefer Link components over navigate()
Use replace for redirects
Use replace for redirects
When redirecting after an action (login, form submit), use
replace: true to avoid polluting the browser history.Leverage relative navigation
Leverage relative navigation
Type your from parameter
Type your from parameter
Always specify
from when using relative navigation outside the route component. This ensures type safety.Use hash navigation sparingly
Use hash navigation sparingly
Navigation Flow
Understanding the navigation lifecycle:- Navigation initiated - Via
Link,navigate(), or browser action - Route matching - Router finds matching route(s)
- beforeLoad runs - Authentication, redirects, context setup
- Loader runs - Data fetching (if needed)
- Navigation completes - New route renders
- onLoad fires - Post-navigation side effects
Next Steps
Loaders
Learn about loading data during navigation
Search Params
Master search parameter navigation patterns
Prefetching
Optimize performance with route prefetching
Type Safety
Explore type-safe navigation patterns