'./src/App.js Attempted import error: 'Switch' is not exported from 'react-router-dom'
I can't figure it out why doesn't work. I have uninstalled the react-router-dom package and reinstalled it, but I always have the same error.
The error I'm getting: ./src/App.js Attempted import error: 'Switch' is not exported from 'react-router-dom'.
This is my code. I hope that someone could give me a hand, Thanks in advance
import './App.css';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import NavBar from '../src/Components/UI/NavBar/NavBar';
import Footer from '../src/Components/UI/Footer/Footer';
import Home from './Components/Views/Home/Home';
function App() {
return (
<Router>
<NavBar/>
<Switch>
<Route path="/">
<Home/>
</Route>
</Switch>
<Footer/>
</Router>
);
}
export default App;
Solution 1:[1]
I had the same problem and took me forever. but mainly as you have asked recently the React-router-dom has been updated and they have removed Switch So try installing below code again. its version 5.
npm install react-router-dom@5
Solution 2:[2]
If you accidentally updated react-router-dom
to version 6 it no longer exports a Switch
component. It was replaced by a Routes
component that must directly wrap/render the Route
components.
Swap the
Switch
for theRoutes
component.import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; ... function App() { return ( <Router> <NavBar/> <Routes> <Route path="/" element={<Home/>} /> </Routes> <Footer/> </Router> ); }
Follow the Upgrading from v5 to migrate your project from v5 to v6 across your app as there were quite a few breaking component API changes.
Revert back to
react-router-dom
v5.- Run
npm un -s react-router-dom
- Run
npm i -s [email protected]
- Run
Solution 3:[3]
I have also faced the same problem. I had version 6 installed. Then I have found out that Switch
was replaced by Routes
. But changing to Routes was messing with my front-end. So I uninstall the version and went back to previous version by these commands:
npm uninstall react-router-dom
npm install [email protected]
This has solved my problem with 'Switch is not exported error'.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MRX18 |
Solution 2 | Drew Reese |
Solution 3 | Rashed Sabbir |