# Fix "'Switch' is not exported from 'react-router-dom'" after upgrading to React Router 6

Yes. An agent ports Switch, useHistory and exact to Routes, useNavigate and element, checks the exports of the installed package, and runs the build before a PR.

[Run this on Rush](https://cloud.getrush.ai/run/fix-switch-is-not-exported-from-react-router-dom/react-router/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

React Router 6 removed `Switch`, `useHistory`, `Redirect` and `withRouter` and gave routes a new shape, so the first build after the upgrade stops in the bundler with `Attempted import error: 'Switch' is not exported from 'react-router-dom'.` It is the first of several: `useHistory` is gone from the same module, `component={}` and `exact` are no longer route props, and nested JSX inside a `Route` moved to `element`. A check against the installed packages makes the list exact: 6.30.6 and 7.18.5 export `Routes`, `useNavigate`, `Navigate` and `BrowserRouter` and neither `Switch` nor `useHistory`, while 5.2.0 exports both of the old names and not `Routes`.

nerdify/db-schema-diagram, a small React app that renders ER diagrams from a dbdiagram-style text, shows the whole pattern in one file. `package.json` pins `react-router 5.2.0` and `react-router-dom 5.2.0` exactly, beside `react-scripts 3.4.1`, React 16.13 and a `react-app-rewired` build; `.nvmrc` names Node 12.16.1 and `yarn.lock` resolves react-router-dom to 5.2.0. `src/App.js` imports `BrowserRouter as Router, Switch, Route, useParams, useHistory` on lines 8 to 14. The `Home` component reads `schema_id` with `useParams()` on line 285 and takes `useHistory()` on line 286, and after creating a new schema it calls `history.push(`/${globalId}`)` on line 327. The `App` component on lines 372 to 378 renders `<Router><Switch><Route exact path="/:schema_id?"><Home /></Route></Switch></Router>`, one route with an optional segment. `index.js` still mounts with `ReactDOM.render`.

The agent begins by pinning the error to the package rather than the bundler. It installs the requested react-router-dom in a clean copy, lists the module's exports from Node, and records which of the five imported names are missing, so the pull request shows the full set before any line changes. It reads the peer dependency range of the chosen release against the React version in `package.json`, since 6.x accepts React 16.8 and later while 7.x needs React 18, and says which it installed and why. It also reads the one route's path, `/:schema_id?`, and checks the installed release's documentation for optional segments, which v6 reintroduced in 6.5.0, before keeping the path as it is.

Then it ports the file in place. `Switch` becomes `Routes`; the route becomes `<Route path="/:schema_id?" element={<Home />} />` with `exact` dropped, since v6 matches routes exactly by default; `useHistory` becomes `useNavigate`, and `history.push(`/${globalId}`)` becomes `navigate(`/${globalId}`)`. `useParams` is unchanged. It greps `src/` for `Redirect`, `withRouter`, `component=`, `render=` and `useRouteMatch` so a second file would be caught, which in this repository finds nothing. It moves the exact pins to the tested release, regenerates `yarn.lock`, runs `yarn build` on the Node version `.nvmrc` names, and writes a render test that mounts `App` under a `MemoryRouter` at `/` and at `/some-id` and asserts the editor renders, so the optional segment is covered.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Our React build fails with Attempted import error: 'Switch' is not exported from 'react-router-dom' after upgrading React Router. Read package.json, the lockfile and any Node version file, and report the React, react-router-dom and react-scripts versions before and after. In a clean install of the new react-router-dom, list the module's exports from Node and record every imported name the code uses that no longer exists, so the pull request lists all of them, not just the first the bundler reported. Check the chosen release's peer dependency on React against the React version in package.json and say which react-router-dom major you installed and why. Then port every usage: Switch to Routes, Route children or component props to element, drop exact, useHistory to useNavigate with history.push and history.replace rewritten, Redirect to Navigate, withRouter to hooks, and useRouteMatch to its v6 equivalents. For each route path with an optional segment, splat or regex, say whether the installed release supports it and change it only if it does not. Search the whole source tree so a second file is not missed. Update the pins, regenerate the lockfile with the repository's package manager, run the production build on the Node version the repository names, and add a render test that mounts the app under MemoryRouter at each route the app defines. Include the build and test output. Do not change components, styles or behaviour beyond the router API. Open a pull request that lists each changed line, the versions before and after, and what could not be verified without a browser.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes `package.json`, the lockfile, the file that defines the routes, and adds one test. The description opens with the export check: the five names the code imports, each marked present or absent in the installed release, and the peer dependency line that decided between React Router 6 and 7 for the project's React version. The bundler's one-line error becomes a complete list.

The diff to the routes is a handful of lines, and the reviewer checks them against the old file: that each `Route` keeps its path, that `exact` was removed rather than translated into something, that every `history.push` became a `navigate` with the same path, and that the search for `Redirect`, `withRouter` and `component=` was run across `src/` and came back empty or with every hit changed. The build output and the render test for each route sit under that.

What the run cannot prove is listed last. The production bundle was built and the routes render in a test, but no browser clicked through the app, so the first visit to a deep link and the navigation after creating a record are for the reviewer.

## Run it on every dependency update pull request

A router major arrives as a dependency update pull request whose build fails. Create an automation on the repository's Project with the prompt above and a `github` trigger on `pull_request.opened` and `pull_request.synchronize`, with a prompt that tells the agent to stop with a one-line answer when the pull request does not touch `package.json`. The pull request's title and description follow the prompt as event text, so the agent knows which package moved before it clones, and it opens its own pull request with the port rather than pushing to that branch. A pull request a bot opens fires no `github` trigger, so a Dependabot or Renovate bump is caught by the `schedule` trigger below instead.

A repository with no CI, like the example, is covered by a `schedule` trigger with a monthly `cron`: the agent installs on the Node version the repository names, runs the build, and opens a pull request only when it fails.

## Why run it on Rush?

A bundler stops at the first missing export, so the usual loop is fix one name, rebuild, find the next. The agent lists the installed module's exports once and compares them with every name the code imports, then ports all of them in one pass, with the output of that check and of the build kept in the session. The pull request carries the list and the proof instead of a sequence of commits named after each error.

The version question is settled by evidence too. Whether a project on React 16 can take React Router 7 is a peer dependency line, and the agent reads it from the package it installed rather than from memory, so the choice of major is stated in the description with its reason. Many small apps on the same template can be ported the same way, each in its own sandbox, each with its own build output.

## Frequently asked questions

### Can I keep React Router 5 instead?

Yes, by keeping the pin; the example pins 5.2.0 exactly and only breaks after a deliberate bump. Staying means the same port later, and React Router 5 does not get the data APIs or the fixes of 6 and 7.

### Which major should I move to, 6 or 7?

The one your React version allows. In the run, react-router-dom 6.x declares a peer dependency on React 16.8 or later, so a React 16 app can take 6, while 7 needs React 18. The agent reads the installed package's peer dependency and says which it chose and why.

### Does the optional segment in my route path still work?

React Router 6 reintroduced optional segments, written as `/:id?`, in 6.5.0, so a route like `/:schema_id?` can stay as it is on any current release. The agent checks the installed version against that and changes a path only when the release does not support it.

### What replaces history.push?

`const navigate = useNavigate()` and `navigate(path)`. `history.replace(path)` becomes `navigate(path, { replace: true })`. The agent rewrites each call and keeps the path expression unchanged.

### Does the agent change my components or styles?

No. The request limits the port to the router API, so components, state and styles stay as they are. The only new file is a render test that mounts the app at each route under `MemoryRouter`.

## More React Router jobs

- [Turn a Labeled GitHub Issue Into a Pull Request on a React Router App](https://getrush.ai/run/github-issue-to-pr/react-router/)
- [Review Pull Requests on a React Router App](https://getrush.ai/run/review-pull-requests/react-router/)
- [Upgrade npm Dependencies in a React Router App](https://getrush.ai/run/upgrade-npm-dependencies/react-router/)

## More jobs like this: Fix a bug from an alert

- [Fix "It looks like you're trying to use `tailwindcss` directly as a PostCSS plugin" after Tailwind CSS 4](https://getrush.ai/run/fix-tailwindcss-directly-as-postcss-plugin/tailwind/)
- [Fix "ImportError: cannot import name 'Filters' from 'telegram.ext'"](https://getrush.ai/run/fix-telegram-ext-filters-import-error/python-telegram-bot/)
- [Fix "Provider registry.terraform.io/hashicorp/template v2.2.0 does not have a package available for your current platform, darwin_arm64"](https://getrush.ai/run/fix-template-provider-darwin-arm64/terraform/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
