Matthew C.
—After upgrading React Router from version 5 to version 6, I’ve been getting the following error:
Attempted import error: 'Switch' is not exported from 'react-router-dom'.
The error looks like this in Vite:
Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/react-router-dom.js?v=41e7aa05' does not provide an export named 'Switch'
React Router version 6 has several breaking changes from version 5, and the above error occurs due to one of them.
Specifically, the error is caused by the <Switch>
component, which is used to render a specific route:
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; function App() { return ( <Router> <Switch> <Route exact path="/" component={Home} /> <Route exact path="/about" component={About} /> <Route path="*" component={NotFound} /> </Switch> </Router> ); } export default App
After upgrading to React Router 6, you should replace the <Switch>
component with the <Routes>
component.
The <Routes>
component functions like the <Switch>
component, but has several improvements, such as relative <Route>
s and <Link>
s.
The <Route>
component has an element
prop, instead of render
or component
props. This change was effected to make the <Route>
component’s API less verbose and simplify its use with React <Suspense>
, as explained in the React Router docs.
When you replace the <Switch>
component with the <Routes>
component, be sure to use the element
prop instead of the render
or component
props:
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; function App() { return ( <Router> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="*" element={<NotFound />} /> </Routes> </Router> ); } export default App
There are several other breaking changes you may encounter when moving from version 5 to version 6, so take a look at the React Router version 5 to version 6 upgrade guide to learn how you can incrementally upgrade. There’s also a backward compatibility package that allows you to incrementally migrate to version 6 by running version 5 and version 6 in parallel. Once all of your components are only using the version 6 APIs, your app no longer needs the compatibility package and can run on version 6 alone.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.