Switch is not exported from react-router-dom

Switch is in fact a react-router-dom package exported component. Instead of rendering all matching routes, it is just used to render the first matching <Route> inside it.

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

Switch is indeed an exported component from the react-router-dom package

Sometimes this error would have come if the switch had not been imported. If so, then you can import the switch as shown in the code. This is a common problem, it can be easily solved.


import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

More info about React Router dom

Even after doing this, the problem is not getting solved and if you are facing the problem in the evening, then do not panic. I am also new to React router, I also came across this issue. I was worried even if I did Google, I didn’t get the solution.

This issue is caused by the version of react-router-dom. So you don’t have to do much, you install the new versions by uninstalling the old router dom, this will solve your problem. After uninstalling you don’t have to do much go to your react app folder and open the terminal by shift+right click. In the terminal, you run the code given below. This will uninstall you Router, Dom.

How to uninstall React Router Dom

npm uninstall react-router-dom

or

yarn remove react-router-dom

After uninstalling, you should install the latest versions of React. It’s currently 5.3.0. If you are seeing this post after a year, then you can see the latest versions on Google. To add new versions, you need to run the following command in the terminal.

CodeIgniter installation tutorial steps for beginners

How to install the latest version of React Router Dom

As a prerequisite for using Switch in your React application, install react-router-dom. To accomplish this, issue the following command:

npm install react-router-dom@5.3.0

or

yarn add react-router-dom@5.3.0

Then, you can import Switch from react-router-dom in your code as follows:

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

// Your code...

<Switch>
  <Route exact path="/" component={Home} />
  <Route path="/about" component={About} />
  <Route path="/contact" component={Contact} />
</Switch>

The Switch component in this illustration encloses several Route components. Based on the current URL, it renders the first matched Route. If the URL matches the route specified by a Route, that component will be rendered.

Please make sure you have the most recent version of react-router-dom installed if you are having trouble getting Switch to export from react-router-dom. Try upgrading it by performing:

npm update react-router-dom

or

yarn update react-router-dom

This should ensure you have the latest version of the package with the Switch component available.

Need Help from experts? Get free help. Click here

After installing the new react router dom, your problem will be solved. Now open your app again. According to my, doing this should solve your problem. If you are still facing a problem then comment to me. I will also help you out.