Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch' is not exported from 'react-router-dom'

Tags:

reactjs

In package.json file react-router-dom dependencies added. App component wrapped by BrowswerRouter , but when I wrap route by switch it says the following error Switch' is not exported from 'react-router-dom'. I deleted the package.json.lock ,node modules, installed npm again and npm install @babel/core --save. Still not working. I successfully wasted 6 hour for this. Can you please help me to fix this? why it's not importing?

Index.js

import {BrowserRouter} from 'react-router-dom';

ReactDOM.render(
  <BrowserRouter>
     <App />
  </BrowserRouter>,
  document.getElementById('root')
);

App.js:

 import logo from './logo.svg';
import './App.css';
import React from 'react';
import {Switch,Route,Link} from 'react-router-dom';
import Home from './Home';
class App extends React.Component {
  componentDidMount(){
    alert('mounting');
  }
  componentDidUpdate(){
    alert('updated');
  }
 render(){
  return (
    
    <div className="App">
     
    <div>
      <Link to="/">Home</Link>
    </div>

    <hr />

    <Switch>
      <Route exact path="/">
        <Home/>
      </Route>
    </Switch>
 
  </div>
 
);
 }
}

export default App;

import React from 'react';

    const Home = () => {
    return <h1>Home</h1>;
  };
  
  export default Home;

package.json

"dependencies": {
    "@babel/core": "^7.16.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^6.0.0",
    "react-router-dom": "^6.0.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
like image 303
enamul haque Avatar asked Nov 04 '21 17:11

enamul haque


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> .

Is Switch available in react-router-dom?

Users will not be able to find Switch in react-router-dom . They need to install versions up to 5 or below 5. Try the below one, which will work. If the user uses routes instead of Switch, it may not work.

How do I fix attempted import error Redirect is not exported from react-router-dom?

To solve the error "export 'Redirect' (imported as 'Redirect') was not found in 'react-router-dom'", use the Navigate component instead of Redirect , e.g. <Navigate to="/dashboard" replace={true} /> . The Navigate component changes the current location when it's rendered. Copied!


3 Answers

Using Routes instead of Switch in react-router v6

You are using react-router-dom version 6, which replaced Switch with the Routes component

import {
  BrowserRouter,
  Routes, // instead of "Switch"
  Route,
} from "react-router-dom";

// ...

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

Note that you now also pass your component as the element prop instead of using children.

like image 72
Joschua Schneider Avatar answered Oct 21 '22 01:10

Joschua Schneider


if you want to use Switch then install react-router-dom version 5. Switch is replaced in react-router-dom version 6.

npm install react-router-dom@5

like image 22
Taib Islam Dipu Avatar answered Oct 21 '22 02:10

Taib Islam Dipu


Users will not be able to find Switch in react-router-dom. They need to install versions up to 5 or below 5. Try the below one, which will work. If the user uses routes instead of Switch, it may not work.

npm install react-router-dom@5
like image 3
Chetan Kotwal Avatar answered Oct 21 '22 01:10

Chetan Kotwal