Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Switch, Redirect or useHistory missing after a React Router upgrade

ErrorAttempted import error: 'Switch' is not exported from 'react-router-dom'

Short answer

React Router v6 removed Switch. Its job moved to the Routes component, and Route now takes an element prop instead of component or render. Replace Switch with Routes, pass JSX to element, drop exact, and swap Redirect and useHistory for Navigate and useNavigate. Or install a v5 release.

  • Applies to react-router-dom 6.30, React 18, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the installed major version. If it is 6 or higher, your code is written for v5 and needs the changes below.

    npm ls react-router-dom
  2. 2Match what you see, then jump to its fix

    1. 1Attempted import error: 'Switch' is not exported from 'react-router-dom'Switch was replaced by RoutesFix
    2. 2The page renders blank, or the route never matches after Switch was replacedRoute still uses component, render, children or exactFix
    3. 3Attempted import error: 'useHistory' is not exported from 'react-router-dom'useHistory and Redirect were removed tooFix

What does the error look like?

  • Attempted import error: 'Switch' is not exported from 'react-router-dom'
  • Attempted import error: 'useHistory' is not exported from 'react-router-dom'
  • Attempted import error: 'Redirect' is not exported from 'react-router-dom'

Cause 1 of 3

Switch was replaced by Routes

1Check

Search the source for the old component. Any file that imports Switch is still v5 code.

grep -rn "Switch" src/

2Fix

Import Routes and use it where Switch was. The v6 guide says every Switch has to be converted. Routes also picks the best match instead of the first one in order, so the order of siblings no longer matters.

// before (v5)
import { Switch, Route } from "react-router-dom";

// after (v6)
import { Routes, Route } from "react-router-dom";

<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/about" element={<About />} />
</Routes>

3Verify

Start the app and open each route. The import error is gone once no file imports Switch.

grep -rn "Switch" src/ || echo "no Switch left"

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 3

Route still uses component, render, children or exact

1Check

In v6 a Route takes JSX in its element prop. component and render are the v5 way, and exact is gone.

grep -rnE "component=|render=|exact" src/

2Fix

Move the component into element as a JSX tag. Read URL values with hooks such as useParams, because route props are no longer passed in. For NavLink, rename exact to end. For a parent route that owns child routes in another component, end its path with /*.

// before (v5)
<Route exact path="/users/:id" component={User} />

// after (v6)
<Route path="/users/:id" element={<User />} />

function User() {
  const { id } = useParams();
  return <h1>User {id}</h1>;
}

3Verify

Open /users/1 in the browser. The component renders and useParams returns the id.

npm start

Confirmed by reactrouter.com

Did this fix it?

Cause 3 of 3

useHistory and Redirect were removed too

1Check

Find the old navigation APIs. They fail with the same kind of import error as Switch.

grep -rnE "useHistory|Redirect" src/

2Fix

Use useNavigate for code-driven navigation and Navigate for a redirect. A v6 Navigate pushes a new history entry by default, while the v5 Redirect replaced the current one, so add the replace prop to keep the old behaviour.

// before (v5)
const history = useHistory();
history.push("/home");
<Redirect to="/login" />

// after (v6)
const navigate = useNavigate();
navigate("/home");
<Navigate to="/login" replace />

3Verify

Trigger the navigation and check the address bar and the back button.

grep -rnE "useHistory|Redirect" src/ || echo "none left"

Confirmed by reactrouter.com

Did this fix it?

How do I stop it coming back?

  • Read the upgrade guide before raising the major version of a router package, and change one file type at a time.
  • Pin the router to a major version range in package.json instead of an open-ended one.
  • Keep React at 16.8 or newer; the v6 guide says v6 depends on hooks.
  • Let a lockfile decide what CI installs, so a fresh install does not jump versions.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.