Skip to main content

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
The Link component is the primary way to create navigation links.
All parameters are type-checked based on the route definition.
Search parameters are typed based on the route’s validateSearch schema.

Updating Search Params

Update search params while staying on the current route:
The search prop accepts a function that receives previous search params.

Hash Navigation

Navigate to page anchors:

State Passing

Pass state through navigation:
Access state in the destination route via useLocation().state.

Relative Navigation

Navigate relative to the current route path.

From Parameter

Specify the starting route for navigation:
This is required when the link is not rendered within the route it navigates from. Style links based on active state:

Using activeProps

Props are applied when the link’s route is active.

Using activeOptions

Custom Active Styling

Render props receive isActive and isTransitioning booleans.

Programmatic Navigation

Navigate imperatively using the useNavigate hook.

Basic Navigation

The useNavigate hook returns a function defined in packages/router-core/src/useNavigate.ts:4-13:

Replace Navigation

Replace history instead of pushing:
Useful for redirects after login or form submission.

Relative Navigation

Router Methods

The router instance provides navigation methods:
Fine-tune navigation behavior with options.

View Transitions

Use the View Transitions API:
Browser support required. Gracefully degrades if not available.

Reset Scroll

Control scroll behavior:

Hash Scroll Behavior

Customize scroll-to-hash:
Accepts standard ScrollIntoViewOptions. React to navigation changes:

Redirects

Perform server-side or client-side redirects.

Throwing Redirects

Redirects thrown in beforeLoad or loader cancel the navigation.

Route-Based Redirects

Relative Redirects

Use the route’s redirect helper:
For external URLs, use standard anchor tags:
The Link component is for internal routing only.

Preloading

Preload routes before navigation:
Or use the built-in preload options:
See the Prefetching guide for more details.

Best Practices

When redirecting after an action (login, form submit), use replace: true to avoid polluting the browser history.
Relative paths make routes more portable and resilient to URL structure changes.
Always specify from when using relative navigation outside the route component. This ensures type safety.
Hash navigation is useful for jump links, but avoid using it as a primary navigation mechanism.
Understanding the navigation lifecycle:
  1. Navigation initiated - Via Link, navigate(), or browser action
  2. Route matching - Router finds matching route(s)
  3. beforeLoad runs - Authentication, redirects, context setup
  4. Loader runs - Data fetching (if needed)
  5. Navigation completes - New route renders
  6. onLoad fires - Post-navigation side effects
Errors or redirects at any step cancel subsequent steps.

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