Quick Start
This guide will help you create your first TanStack Router application from scratch. You’ll learn how to define routes, create navigation, and set up a working router in just a few minutes.This guide uses React, but the concepts apply to Solid and Vue as well. The main differences are in the import paths and component syntax.
Prerequisites
Before you begin, make sure you have:- Completed the Installation guide
- A React, Solid, or Vue project set up
- Basic knowledge of TypeScript
Choose Your Approach
TanStack Router supports two routing approaches:Code-Based Routing
Define routes programmatically with full control over route configuration. Best for smaller applications or when you need precise control.
File-Based Routing
Automatically generate routes from your file system. Best for larger applications and faster development.
Step 1: Create the Root Route
Every TanStack Router application starts with a root route. This is the layout that wraps all other routes. Create your main application file (e.g.,main.tsx):
The
Outlet component is where child routes will be rendered. It works like a placeholder for nested content.Step 2: Create Route Definitions
Now define your application routes usingcreateRoute:
Understanding Route Configuration
Understanding Route Configuration
Each route created with
createRoute requires:getParentRoute: A function that returns the parent routepath: The URL path for this route (use/for index routes)component: The React component to render for this route
loader, beforeLoad, errorComponent, and many more.Step 3: Build the Route Tree
Combine your routes into a tree structure:addChildren calls:
Step 4: Create the Router Instance
Create your router with the route tree and configuration:The tree of routes created in the previous step.
When to preload routes:
'intent': Preload on hover or focus'render': Preload when the link is renderedfalse: No automatic preloading
Whether to automatically restore scroll position when navigating.
How long (in milliseconds) to cache route loader data.
Step 5: Register Router for Type Safety
Register your router type to enable TypeScript autocomplete and type checking:Step 6: Render the Application
Finally, render your router:Complete Example
Here’s the complete working example:Adding Data Loading
TanStack Router can load data for routes automatically. Add aloader to fetch data before rendering:
Route params like
$postId are automatically typed and validated. TypeScript will enforce the correct parameter names throughout your application.Adding Search Parameters
Handle URL search parameters with type safety:Using the DevTools
The TanStack Router DevTools help you debug your routing:DevTools Features
DevTools Features
The DevTools provide:
- Route Explorer: View your complete route tree
- Current Matches: See which routes are currently active
- Route Loader Data: Inspect data loaded by each route
- Search Params: View and edit current search parameters
- Cache Inspector: Monitor route data cache state
- Performance Metrics: Track route loading and rendering times
Navigation Patterns
TanStack Router provides multiple ways to navigate:Using Link Components
Programmatic Navigation
Navigation with State
Error Handling
Handle errors gracefully with error components:Next Steps
Now that you have a working router, explore more advanced features:File-Based Routing
Automatically generate routes from your file system
Data Loading
Learn advanced data loading patterns and caching
Search Params
Master type-safe URL state management
Authenticated Routes
Protect routes and handle authentication
Common Issues
Routes Not Matching
If your routes aren’t matching:- Ensure paths don’t have trailing slashes (use
/about, not/about/) - Check that child routes are properly added with
addChildren - Verify your root route is registered correctly
TypeScript Errors
If you’re seeing type errors:- Make sure you’ve registered your router type with the
declare moduleblock - Ensure you’re using TypeScript 5.4 or higher
- Try restarting your TypeScript server in your editor
Link Not Active
If active link styling isn’t working:- Use
activeOptions={{ exact: true }}for exact matching - Ensure you’re using
activePropsoractiveClasscorrectly - Check that your route paths match the
toprop exactly
Need more help? Check out our GitHub Discussions or join our Discord community.