本页还没有译文
本页 4 段还没译成简体中文,下面显示的是英文原文。Migration from React Router Checklist
还没翻译译自英文(tanstack.com)本页 4 段都还没译
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.
- At this point I don’t know if you can do a gradual migration, but it seems likely you could have multiple router providers, not desirable.
- The api’s between React Router and TanStack Router are very similar and could most likely be handled in a sprint cycle or two if that is your companies way of doing things.
- 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 - This was necessary in the case of Supertokens. Supertoken has a specific setup with React Router and a different setup with all other React implementations - Set RouterProvider and pass it the router as the prop
- Replace all instances of React Router
Linkcomponent with@tanstack/react-routerLinkcomponent- Add
toprop with literal path - Add
paramsprop, where necessary with params like soparams={{ orderId: order.id }}
- Add
- Replace all instances of React Router
useNavigatehook with@tanstack/react-routeruseNavigatehook- Set
toproperty andparamsproperty where needed
- Set
- 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.- Instead of using the
useSearchParamshook, use@tanstack/react-routerLink's search property to update the search params state - To read search params you can do something like the following
const { page } = useSearch({ from: productPage.fullPath })
- Instead of using the
- 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- So say we have a route with the path name
orders/$orderid. - In the
useParamshook we would set up our hook like so:const params = useParams({ from: "/orders/$orderId" }) - Then wherever we wanted to access the order id we would get it off of the params object
params.orderId
- So say we have a route with the path name
AI 翻译查看英文原文