Migration Checklist: React Router to TanStack Router
A step-by-step checklist for migrating a project from React Router to TanStack Router.
PRID: 1602
VERIFIED
2 min read
title: Migration from React Router Checklist toc: false
If your UI is blank, open the console, and you will probably have some errors that read something along the lines of cannot use 'useNavigate' outside of context . This means there are React Router api’s that are still imported and referenced that you need to find and remove. The easiest way to make sure you find all React Router imports is to uninstall react-router-dom and then you should get typescript errors in your files. Then you will know what to change to a @tanstack/react-router import.
Here is the example repo
- Install Router -
npm i @tanstack/react-router(see detailed installation guide) - Optional: Uninstall React Router to get TypeScript errors on imports.
- Create Routes for each existing React Router route we have
- Create root route
- Create router instance
- Add global module in main.tsx
- Remove any React Router (
createBrowserRouterorBrowserRouter),Routes, andRouteComponents from main.tsx - Optional: Refactor
renderfunction for custom setup/providers - The repo referenced above has an example - Set RouterProvider and pass it the router as the prop
- Replace all instances of React Router
Linkcomponent with@tanstack/react-routerLinkcomponent - Replace all instances of React Router
useNavigatehook with@tanstack/react-routeruseNavigatehook - Replace any React Router
Outlet's with the@tanstack/react-routerequivalent - If you are using
useSearchParamshook from React Router, move the search params default value to the validateSearch property on a Route definition. - If using React Router’s
useParamshook, update the import to be from@tanstack/react-routerand set thefromproperty to the literal path name where you want to read the params object from