Switch, Redirect or useHistory missing after a React Router upgrade
Attempted 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.
Fix it fast
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-dom2Match what you see, then jump to its fix
- 1
Attempted import error: 'Switch' is not exported from 'react-router-dom'Switch was replaced by RoutesFix - 2
The page renders blank, or the route never matches after Switch was replacedRoute still uses component, render, children or exactFix - 3
Attempted import error: 'useHistory' is not exported from 'react-router-dom'useHistory and Redirect were removed tooFix
- 1
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
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 startConfirmed by reactrouter.com
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix