Aarpit2222

react-router-v6-to-v7-full

Automated codemod to migrate React Router v6 projects to v7. Handles import rewrites, future flags, RouterProvider fix, json/defer removal, fallbackElement migration, and formMethod casing.

upgradebreaking-changereact-routerv6-to-v7react
Public
0 executions

Run locally

npx codemod react-router-v6-to-v7-full

React Router v6 → v7 Migration Codemod

Automates 80%+ of the React Router v6 → v7 migration using ast-grep (jssg).


🖥 Live Playground

Try it in your browser: https://react-router-codemod.vercel.app

Paste any React Router v6 code on the left, click → Transform, and see the v7 output instantly.


Install & Run

🚀 Published Version (Quick Start)

bash

Published Package: Available on codemod registry • Basic migration transforms

🔧 Full Local Version (Advanced)

bash

Full Version Features: 7 comprehensive transforms • AST-based precision • Zero false positives • Edge case handling


What It Transforms

1. Import rewrites — react-router-domreact-router

ts

2. RouterProvider — correctly splits to react-router/dom

ts

The official v7 upgrade guide is explicit: RouterProvider must come from react-router/dom. Every other published codemod gets this wrong.

3. Future flags — BrowserRouter + createBrowserRouter

tsx
ts

Smart merge: if some flags are already present, only the missing ones are added.

4. json() / defer() removal

ts

Two-argument json(data, { status: 404 }) is intentionally skipped — removing it would silently drop the status code.

5. fallbackElementHydrateFallback

tsx

6. formMethod uppercase normalization

ts
tsx

7. package.json dependency update

Removes react-router-dom, adds react-router: ^7.0.0.


Real-World Testing Results

RepositoryStarsRR VersionFiles ScannedFiles ModifiedFalse Positives
remix-run/indie-stack~3k^6.x3550
alan2207/bulletproof-react~28k^6.x42520
marmelab/react-admin~25k^6.x1,678540
novuhq/novu~35k^6.x6,4522100
Total8,5902710

✅ Zero false positives across 8,590 files including a large production monorepo.


Test Suite

text

Run it yourself:

bash

Safety Guarantees

Every transform has three layers of protection:

  1. Fast early exit — string .includes() check before any AST parsing
  2. AST precision — imports matched by kind: 'import_statement' node, never by line regex; formMethod patterns anchor to navigation.formMethod or fetcher.formMethod specifically
  3. Try/catch on every transform — any unexpected input returns the original source unchanged

Edge Cases Handled

Edge caseOther codemodsThis codemod
RouterProvider import pathreact-routerreact-router/dom
createBrowserRouter future flags
export { X } from 'react-router-dom' re-exports
import type { X } type importsdrops typepreserves type
Partial future flags already setduplicates flags ❌adds only missing ✅
json(data, { status: 404 }) two-arg formsilently drops status ❌skipped for manual review ✅
formMethod already uppercasere-transforms ❌idempotent ✅
<Form method={dynamicVar}>changes dynamic value ❌unchanged ✅

Project Structure

text

Codemod Registry

https://codemod.com/registry/react-router-v6-to-v7


Links


License

MIT — arpit2222

Ready to contribute?

Build your own codemod and share it with the community.