Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

I don't know why I am getting this error and I can't find an answer for it anywhere. I have uninstalled the react-router-dom package and reinstalled it, but it continues to tell me that the switch module is not exported from react-router-dom. Here's my code.

The error I'm getting:

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

Code

import React from 'react';
import './App.css';
import NavBar from './components/navbar.js';
import Footer from './components/footer.js';
import Home from './components/pages/homepage/home.js';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="app-container">
        <NavBar />
        <Switch>
          <Route path="/home" component={Home} />
        </Switch>
        <Footer />
      </div>
    </Router>
  );
}

export default App;
like image 962
ajesamann Avatar asked Oct 07 '22 21:10

ajesamann


People also ask

How do I fix error Switch is not exported from react-router-dom?

To solve the error "export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'", import Routes instead of Switch and wrap your <Route /> components with a <Routes> component, e.g. <Routes><Route path="/about" element={<About />} /></Routes> .

How do you fix attempted import error routes is not exported from react-router-dom?

To Solve Attempted import error: 'Switch' is not exported from 'react-router-dom' Error You Just need to Downgrade react-router-dom to 5.2. 0 So that First of all Just Uinstall react-router-dom With the help Of This Command: npm uninstall react-router-dom And Then Install 5.2.

How do I fix attempted import error in react JS?

The React. js "Attempted import error 'X' is not exported from" occurs when we try to import a named import that is not present in the specified file. To solve the error, make sure the module has a named export and you aren't mixing up named and default exports and imports.

Is Switch no longer in react router?

Switch is replaced in react-router-dom version 6. Show activity on this post. Users will not be able to find Switch in react-router-dom . They need to install versions up to 5 or below 5.


Video Answer


2 Answers

In react-router-dom v6, "Switch" is replaced by routes "Routes". You need to update the import from

import { Switch, Route } from "react-router-dom";

to

import { Routes ,Route } from 'react-router-dom';

You also need to update the Route declaration from

<Route path="/" component={Home} />

to

<Route path='/' element={<Home/>} />

In react-router-dom, you also do not need to use the exact in the Route declaration.

For more information, you can visit the official documentation: upgrade to react-router-dom v6

like image 115
prabhat kumar jena Avatar answered Oct 14 '22 00:10

prabhat kumar jena


If you are using react-router-dom v6, it looks like Switch has been replaced with Routes.

like image 33
luke.mas Avatar answered Oct 13 '22 22:10

luke.mas