Basic information
About tanstack-router-migration
Migrate React applications from React Router to TanStack Router with file-based routing. Use when user requests: (1) Router migration, (2) TanStack Router setup, (3) File-based routing implementation, (4) React Router replacement, (5) Type-safe routing, or mentions 'migrate router', 'tanstack router', 'file-based routes'.
tanstack-router-migration is an agent skill listed in the AddMCP.net directory, published by redpanda-data. Runtime: TypeScript. The repository has 4,349 GitHub stars. Source code is hosted on github.com. Related topics: apache-kafka, dataops, go, kafka, kafka-gui.
Overview
What is tanstack-router-migration?
name: tanstack-router-migration description: "Migrate React applications from React Router to TanStack Router with file-based routing. Use when user requests: (1) Router migration, (2) TanStack Router setup, (3) File-based routing implementation, (4) React Router replacement, (5) Type-safe routing, or mentions 'migrate router', 'tanstack router', 'file-based routes'." allowed-tools: Read, Write, Edit, Bash, Glob, Grep, Task
React Router to TanStack Router Migration
Migrate React applications from React Router to TanStack Router with file-based routing. This skill provides a structured approach for both incremental and clean migrations.
Critical Rules
ALWAYS:
- Use file-based routing with routes in
src/routes/directory - Use
fromparameter in all hooks for type safety (useParams({ from: '/path' })) - Validate search params with Zod schemas using
@tanstack/zod-adapter - Configure build tool plugin before creating routes
- Register router type for full TypeScript inference
- Use
fallback()wrapper for optional search params
NEVER:
- Edit
routeTree.gen.ts(auto-generated file) - Use React Router hooks in new code during migration
- Forget the
fromparameter (loses type safety) - Use string-only validation for search params
- Skip the build plugin configuration
Dependencies
# Core dependencies
bun add @tanstack/react-router @tanstack/zod-adapter
# Build plugin (choose one based on your bundler)
bun add -d @tanstack/router-plugin
# Optional integrations
bun add nuqs # URL state management
bun add @sentry/react # Error tracking with router integrationMigration Phases
Quick Reference
Route File Naming
| Pattern | File | URL | |---------|------|-----| | Index route | topics/index.tsx | /topics | | Dynamic param | topics/$topicName.tsx | /topics/:topicName | | Nested dynamic | topics/$topicName/edit.tsx | /topics/:topicName/edit | | Pathless layout | _layout.tsx | (no URL segment) | | Catch-all | $.tsx | /* |
Common Zod Patterns
import { fallback, zodValidator } from '@tanstack/zod-adapter';
import { z } from 'zod';
const searchSchema = z.object({
// Optional string with undefined default
tab: fallback(z.string().optional(), undefined),
// Optional number with default value
page: fallback(z.number().optional(), 1),
// Required string
id: z.string(),
// Enum with default
sort: fallback(z.enum(['asc', 'desc']).optional(), 'asc'),
// Boolean
expanded: fallback(z.boolean().optional(), false),
});
Trailing Slash in from Parameter
The from parameter must exactly match the route path as defined:
// Index routes (files named index.tsx) include trailing slash:
useParams({ from: '/topics/$topicName/' }) // Route: topics/$topicName/index.tsx
// Non-index routes do NOT include trailing slash:
useParams({ from: '/topics/$topicName/edit' }) // Route: topics/$topicName/edit.tsx
Type-Safe Navigation
// With params
<Link to="/topics/$topicName" params={{ topicName: 'my-topic' }}>
View Topic
</Link>
// With search params
<Link to="/topics" search={{ page: 2, sort: 'desc' }}>
Page 2
</Link>
// Programmatic navigation
const navigate = useNavigate({ from: '/topics/$topicName' });
navigate({
to: '/topics/$topicName/edit',
params: { topicName },
search: { tab: 'settings' },
});Checklist
Pre-Migration
- [ ] Dependencies installed (
@tanstack/react-router,@tanstack/router-plugin,@tanstack/zod-adapter) - [ ] Build tool plugin configured
- [ ] Linter configured to allow
$param.tsxnaming - [ ]
src/routes/directory created
Route Migration
- [ ]
__root.tsxcreated with providers and layout - [ ]
index.tsxcreated for root redirect - [ ] All routes migrated to file-based structure
- [ ] Search params validated with Zod schemas
- [ ]
staticDataadded for titles/icons
Hook Migration
- [ ] All
useParamscalls updated withfromparameter - [ ] All
useSearchParamsreplaced withrouteApi.useSearch() - [ ] All
useNavigatecalls updated withfromparameter - [ ] All
Linkcomponents verified working
Testing
- [ ]
renderWithFileRoutesutility created - [ ] Vitest configured with TanStack Router plugin
- [ ] Existing tests updated to use new utilities
Integrations
- [ ] Sentry integration configured (if used)
- [ ] nuqs adapter wrapped in root layout (if used)
Cleanup (after full migration)
- [ ] React Router dependencies removed
- [ ] Legacy route definitions deleted
- [ ] BrowserRouter wrapper removed
- [ ] RouterSync component removed
Common Pitfalls
- Missing
fromparameter - Always specifyfromin hooks for type safety - Forgetting
fallback()wrapper - Optional search params needfallback(z.string().optional(), undefined) - Trailing slash inconsistency - Configure
trailingSlash: 'never'and be consistent - Editing routeTree.gen.ts - Never edit; it's auto-generated on file changes
- Missing build plugin - Routes won't generate without the bundler plugin
- Async navigation warnings -
navigate()returns Promise; usevoid navigate()or await it - Using
<Navigate>for section redirects - UsebeforeLoadwiththrow redirect()instead to prevent navigation loops in embedded mode:
beforeLoad: () => {
throw redirect({ to: '/section/$tab', params: { tab: 'default' }, replace: true });
}
- Trailing slash in
fromparameter for index routes - Index routes (files namedindex.tsx) require trailing slash infrom:
// Index route: /topics/$topicName/index.tsx
useParams({ from: '/topics/$topicName/' }) // ✅ Correct (trailing slash)
useParams({ from: '/topics/$topicName' }) // ❌ Wrong
- Missing HistoryState extension - Extend
HistoryStateinterface for typed navigation state (see Phase 3)
Videos
Tutorials, guides, and showcases related to tanstack-router-migration. The player loads when you press play.
Videos belong to their creators. Relevance is approximate; check the channel and description before following setup steps.
Comments
No comments yet
Be the first to share feedback about this listing. Sign in to leave a comment.
Sign In