708

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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ajesamann
  • 7,160
  • 3
  • 6
  • 9

39 Answers39

1267

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

Addison
  • 7,322
  • 2
  • 39
  • 55
prabhat kumar jena
  • 12,819
  • 1
  • 10
  • 10
  • 78
    Attention to the change `Home` to ``, I didn't notice it at first look. – giovannipds Nov 16 '21 at 17:29
  • Also attention to `Route`s having `exact`, that prop was kind of removed. – giovannipds Nov 22 '21 at 18:33
  • 4
    Does anyone know the rationale for these changes to react-router-dom? – InterstellarX Jan 12 '22 at 05:59
  • Oh, yeah! It perfectly answers the question. However, make sure to notice the change in component to 'element', otherwise will result in a silent warning in the browser console "Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page." – Arun Ramachandran Mar 06 '22 at 17:44
  • Unfortunately, this broke my code. See here > https://stackoverflow.com/questions/72049211/changing-switch-routes-and-redirect-navigate-broke-protected-hoc-route. Looks like I'll go back to using version 5. – Fiddle Freak Apr 28 '22 at 19:53
  • 1
    Componet does not work. Replace with element – murage kibicho Jun 03 '22 at 02:14
  • I wonder why we do not need `exact`. If `Routers` behaves like the old `Switch` (going down the list of paths and render whatever it sees matches partially, without exact) then if I put my homepage with path "/" as a Route before my about page with path "/about", even with `url/about`, I think homepage will be rendered? – Reimirno Jul 06 '22 at 23:15
  • for me it worked but I changed `component={}` to `Component={Home}` – Muhammad Jawad Mansoor Aug 03 '23 at 11:26
231

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
luke.mas
  • 2,335
  • 1
  • 6
  • 12
173

This is an example using react-router-dom V6

import React from 'react'
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'

import '../styles/global.css'

import Layout from '../containers/Layout'
import Home from '../pages/Home'
import Login from '../containers/Login'
import RecoveryPassword from '../containers/RecoveryPassword'
import NotFound from '../pages/NotFound'

const App = () => {
  return (
    <Router>
      <Layout>
        <Routes>
          <Route exact path="/" element={<Home/>}/>
          <Route exact path="/login" element={<Login/>}/>
          <Route exact path="/recovery-password" element={<RecoveryPassword/>}/>
          <Route path="*" element={<NotFound/>}/>
        </Routes>
      </Layout>
    </Router>
  );
}

export default App;
Jean Leon
  • 1,873
  • 1
  • 8
  • 7
116

I also faced the same problem, and I searched towards the Internet so much, but I didn't get any answer according to my question.

So I uninstalled the version 6 of react-router-dom:

npm uninstall react-router-dom

And installed version 5.2.0 of react-router-dom:

npm install react-router-dom@5.2.0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Khizr AA Shaikh
  • 1,177
  • 1
  • 4
  • 3
  • 1
    This work perfectly , among all the answer this is the easiest way of doing by downgrading react-router-dom to 5.2.0 , but in my opinion we need to know the new syntax as well . Thanks – vicky kumar Jun 03 '22 at 19:23
66

Syntax has changed

Old Syntax

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

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

New Syntax:

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

<Routes>
    <Route path="/home" element={<Home/>} />
</Routes>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AWS PS
  • 4,420
  • 1
  • 9
  • 22
34

In react-router-dom v6, Switch has been replaced with Routes. Use this format:

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

<Router>
    <Routes>
        <Route exact path="/" element={<component />} />
        <Route exact path="/path2" element={<component2 />} />
        <Route exact path="/path3" element={<component3 />} />
    </Routes>
</Router>
Suraj
  • 802
  • 9
  • 7
32

If you are using version 6 of react-router-dom, use this

Also, please see here for reference : https://reactrouter.com/docs/en/v6/upgrading/v5#:~:text=single%20route%20config%3A-,//%20This%20is%20a%20React%20Router%20v6%20app,%7D,-This%20step%20is

// This is a React Router v6 app
import {
  BrowserRouter,
  Routes,
  Route,
  Link,
  Outlet
} from "react-router-dom";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="users" element={<Users />}>
          <Route path="me" element={<OwnUserProfile />} />
          <Route path=":id" element={<UserProfile />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

function Users() {
  return (
    <div>
      <nav>
        <Link to="me">My Profile</Link>
      </nav>

      <Outlet />
    </div>
  );
}
Emmaccen
  • 488
  • 4
  • 7
20

I was able to fix it by changing from Switch to Routes. So you should have something like this:

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

And also you need to change the import from Switch to Routes:

import { Routes, Route } from "react-router-dom";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mateo Verdaguer
  • 339
  • 2
  • 5
16

react-router-dom has updated to version 6. Now they have renamed the <Switch/> component to <Routes/>. There are also many changes.

You should spend sometime to read the documentation. Here is the link for react-router-v6-doc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I followed a quick guide for v5 and got the same error. v6 doc is here: https://github.com/remix-run/react-router/blob/main/docs/getting-started/tutorial.md – Nathan Lo Sabe Dec 26 '21 at 00:16
15

<Switch> is replaced by <Routes>

Before:

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

<Router>
    <Switch>
        <Route />
        <Route />
    </Switch>
</Router>

Now:

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

<Router>
    <Routes>
        <Route />
        <Route />
    </Routes>
</Router>

Just use Routes instead of Switch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ANOOP NAYAK
  • 473
  • 5
  • 8
11

Step 1: Upgrade to React 16.8+ and react-router-dom v5.2:

npm uninstall react-router-dom
npm install react-router-dom@5.2

Step 2: Update the react-router-dom import statement.

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

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

Step 3: Upgrade the syntax and replace "Switch" with "Routes" and "component" with "element"

Change

<Switch>
    <Route path="/home" component={HomePage} />
</Switch>

to

<Routes>
    <Route path="/home" element={<HomePage/>} />
</Routes>

Alternatively you can also downgrade the react-router-version as suggested in other solutions.

However, rather than going backwards, I would suggest to upgrade to latest version of react-router-dom and its syntax.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anon17
  • 199
  • 4
  • 9
10

Change from

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

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

to

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

<Routes>
    <Route path="/home" element={ <Home />} />
</Routes>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jude Okagu
  • 195
  • 2
  • 6
10

I solved my error by changing the way I was rendering my routes to the use of the element.

To:

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
  return (
    <Router>
      <Routes>
        <Route path="/login" caseSensitive={false} element={<Login />} />
        <Route path="/register" caseSensitive={false} element={<Register />} />
        <Route path="/" caseSensitive={false} element={<Home />} />
      </Routes>
    </Router>
  );
};
export default R;

Basically before v6.*:

import React from "react";
import { BrowserRouter as Router, Route, Switch} from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
  return (
    <Router>
      <Switch>
        <Route path="/login">
           <Login />
        </Route>
       <Route path="/register">
           <Register/>
        </Route>

       <Route path="/">
           <Home/>
        </Route>
      </Switch>
    </Router>
  );
};
export default R;

After v6.*

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
  return (
    <Router>
      <Routes>
        <Route path="/login" caseSensitive={false} element={<Login />} />
        <Route path="/register" caseSensitive={false} element={<Register />} />
        <Route path="/" caseSensitive={false} element={<Home />} />
      </Routes>
    </Router>
  );
};
export default R;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
crispengari
  • 7,901
  • 7
  • 45
  • 53
10

In react-router-dom v6 Switch is Replaced with Routes.

And component with element

{componentName} with {}

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Usama Tayyab
  • 121
  • 1
  • 9
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 04 '22 at 13:47
  • This is repeating previous answers, e.g. [Sridhar Rajaram's answer](https://stackoverflow.com/questions/63124161/attempted-import-error-switch-is-not-exported-from-react-router-dom/70003931#70003931). – Peter Mortensen Aug 19 '22 at 01:02
9

Switch is exported from react-router and not react-router-dom (the same goes for Route I think).

known-as-bmf
  • 1,162
  • 8
  • 9
  • React Router Dom has its own components and API https://reactrouter.com/web/guides/quick-start - here's a github issue to elaborate on the "differences" https://github.com/ReactTraining/react-router/issues/4648 – kJs0 Jul 27 '20 at 22:01
  • @kJs0 you are right, I forgot `react-router-dom` re-exports everything from `react-router`. – known-as-bmf Jul 27 '20 at 22:07
  • this actually did fix my error, i installed the react-router package and imported it via react-router, but now I am getting another error stating "Error: Invariant failed: You should not use outside a ", when my switch is definitely inside of a router? Lol – ajesamann Jul 27 '20 at 22:09
  • Make sure both version are exactly the same `react-router` & `react-router-dom` – known-as-bmf Jul 27 '20 at 22:20
9

Write <Routes> instead of <Switch>.

Run this in the terminal:

npm install --save react-router react-router-dom

This helped me. Or check file package.json and add the following right after "react-dom": "^17.0.2",:

 "react-router": "^6.0.0",
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HM Nomaan
  • 131
  • 1
  • 4
9

You have to check npm package version first. To check, run npm info react-router-dom version.

If the package version is >= 6.0.0, you have to change

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

to

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

And also have to change

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

to

<Route path="/home" element={<Home/>} />
Broda Noel
  • 1,760
  • 1
  • 19
  • 38
Md.Shakil Shaikh
  • 347
  • 4
  • 11
8

What is your react-router-dom version?

"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",

If it is above one then you need to remove node-modules and reinstall node-module using npm install --save.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Namrata Sanja
  • 216
  • 2
  • 1
7

If you are using react-router-dom v6, you have to change Switch to Routes as given in the example below:

Instead of importing Switch, import { Switch, ... } from 'react-router-dom';

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

Then, instead of using component = {ComponentName}, use element={<ComponentName/>} as shown below:

import { Routes, Route, ...} from 'react-router-dom';
    .
    .
    .  
    <Routes>
      <Route exact path='/' element={<Home/>}></Route>
    </Routes>
Abebe Kayimo
  • 329
  • 2
  • 7
7

If you are using version 6 of react-router-dom, then you need to update Switch with Routes. The below syntax worked for me:

import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import './App.css';
import Home from './components/Home';

function App() {
  return (
    <div>
      <Router>
        <Routes>
          <Route path="/" exact element={<Home />} />
        </Routes>
      </Router>
    </div>
  );
}

export default App;
Deep
  • 399
  • 3
  • 3
7

I had the same issue. On the project terminal, type the following commands. You will not need to make any changes to your code.

  1. npm uninstall react-router-dom

  2. npm install react-router-dom@5.2.0

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Md. Imrul Kayes
  • 819
  • 6
  • 13
  • 1
    I would never suggest to downgrade. Expecially not a major downgrade. Might be a quick fix but it's dirty. – BonisTech Dec 26 '21 at 16:59
6

Switch has been replaced by Routes.

Source from Update routing (react-router-dom syntax) #1386 (howtographql GitHub)

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

I solved the problem like this:

yarn add react-router-dom@5,3,0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4
import {
      BrowserRouter as Router,
      Routes,
      Route,
      Link
    } from "react-router-dom";
    
    function App() {
      return (
        <>
        <Router className="App">
          <Navbar/>
          <Routes>
            <Route path='/'>
    
            </Route>
          </Routes>
        </Router>
        </>
      );
    }
    
    export default App;
Romil Jain
  • 305
  • 3
  • 5
4

A solution:

Delete the node_modules folder. In the package.json file, change the react-router-dom version (version 6 in my case) to "react-router-dom": "^5.2.1"

Then in the terminal run:

npm install to install the dependencies and then run npm start to relaunch

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hanumanDev
  • 6,592
  • 11
  • 82
  • 146
4

If you are using a newer version of react-router-dom (for example, ^6.2.1 in my case) you should change <Switch> to <Routes> and use <Route> with the component={<SampleComponent />} parameter.

Particularly the code example below:

import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';
import MyComponent from './containers/MyComponent';

export default function AppRoutes() {
    return (
        <Routes>
            <Route exact path="/" component={<MyComponent />}>
            </Route>
        </Routes>
    );
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yahor M
  • 617
  • 8
  • 8
3

If you installed react-router and react-router-dom v6 beta, just uninstall like this:

npm uninstall --save react-router react-router-dom

And install it with this:

npm install --save react-router react-router-dom

It is going to install the latest version that is not beta.

Then we just need to restart Visual Studio Code or whichever text editor you handle it with.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Erick
  • 332
  • 1
  • 2
  • 13
3

This is actually not a problem with you or React or your code. It's just the updated version of react-router-dom. Replace the Switch by Routes.

That’s it. Just use Routes instead of Switch and it works fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

In react-router-dom v6, the switch is replaced by the Routes. Below is the simple example to configure react-router-dom v6.

import * as React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

import Layout from './components/Layout';
import ProtectedRoute from './components/ProtectedRoute';
import LoginPage from './pages/LoginPage';
import WizardPage from './pages/WizardPage';
import RequestsPage from './pages/RequestsPage';

import './App.scss'

export class App extends React.Component {
    public render() {
        const sharedRouteProps = { exact: true, authenticationPath: '/login' };
        const wizardRouteProps = { ...sharedRouteProps, path: "/", component: WizardPage };
        const requestsRouteProps = { ...sharedRouteProps, path: "/requests", component: RequestsPage };

        return (
            <Layout>
              <Router>
                <Switch>
                    <Route exact path='/login' component={LoginPage} />
                    <ProtectedRoute {...wizardRouteProps} />
                    <ProtectedRoute {...requestsRouteProps} />
                </Switch>
              </Router>
            </Layout>
        );
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Madhav
  • 41
  • 2
3

You simply have to do two things:

  1. Replace Switch with Routes.
  2. Use this Route tag
<Route path="/" element={<Component />} />

instead of this one:

<Route>
  path="/"
  <Component />
</Route>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

use Routes instead of Switch if you are using react-router-dom version6

3
import React from "react";
import { Link, BrowserRouter as Router, Route, Routes } from "react-router-dom";
import "./App.css";
import App from "./App";
const RouterPage = () => {
    return (
        <Router>
            <div>
                <nav>
                    <Link to="/">Home</Link>
                    <Link to="/posts">Posts</Link>
                    <Link to="/">About</Link>
                    <Link to="/contact">Contact</Link>
                </nav>
                <Routes>
                    <Route exact path="/" element={<App />}></Route>
                    <Route path="/post" element={<App />} />
                    <Route path="/about" element={<App />} />
                    <Route path="/contact" element={<App />} />
                </Routes>
            </div>
        </Router>
    );
};

you should replace Switch to Routes

tomnyson
  • 189
  • 2
  • 7
2

I got this error after installing react-router-dom v6.0.2.

Replacing Switch with Routes solved the issue:

import {BrowserRouter as Router, Route, Routes} from "react-router-dom";
yaach
  • 386
  • 2
  • 3
2

There has been a breaking change in the react-router-dom package recently. React Router v6 Changelog Permalink

So all you need is to rename the import like so...

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

Cheers!

Shubham Kushwah
  • 545
  • 1
  • 9
  • 16
1

In version 6 of react-router-dom, switch is no longer used and instead you can use the following example: import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'

import { store } from './redux/store.js'
import { Provider } from 'react-redux'

import { Form } from "./component/Form/Form"
import { Navbar } from "./component/Navbar/Navbar"

import { Home } from "./pages/Home/Home"
import { AboutUs } from "./pages/AboutUs/AboutUs"
import { Products } from "./pages/Products/Products"
import { NotFound } from "./pages/NotFound/NotFound"
import { Dashboard } from "./pages/Dashboard/Dashboard"
import { PrivateRoute } from "./pages/PrivateRoute"

const App = () => {
    return (
        <Provider store={store}>
            <Router>
                <Navbar />
                <Routes>
                    <Route path="/" element={<Home />} />
                    <Route path="/login" element={<Form />} />
                    <Route path="/" element={<Form />} />
                    <Route path="/products" element={<Products />} />
                    <Route path="/about-us" element={<AboutUs />} />
                    <Route element={<PrivateRoute />}>
                        <Route path="/dashboard" element={<Dashboard />} />
                    </Route>
                    <Route path="/*" element={<NotFound />} />
                </Routes>
            </Router>
        </Provider>
    )
}
export default App
0

I have faced the same issue and I have removed react-router-dom and then reinstalled it.

The issue occurred because I have upgraded it to the latest version, i.e., react-router-dom v6 and then wanted to downgrade to the previous version, i.e., v5.

I was on Ruby on Rails project and using yarn to manage dependency you can use this command to remove and reinstall it:

yarn remove react-router-dom

yarn add react-router-dom

This should solve the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anand Soni
  • 5,070
  • 11
  • 50
  • 101
0

I was facing the same issue as the issue poster. I tried all things like below:

  • uninstall and install react-router-dom
  • separately imported Switch from react-router-dom

But nothing really worked for me.

Kindly follow the below instructions. I am sure you will not get the error again.

Correct 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, Routes, Route, Link } from "react-router-dom";

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

export default App;

Note: Switch have been replaced with Routes and instead of using component, we need to use element.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I was also facing that issue and finally solved it, by arranging code. I am very new in React.

Following is my App.js code (class base component):

import logo from './logo.svg';
import './App.css';

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Navbar from './components/Navbar';
import News from './components/News';
import { BrowserRouter as Router, Route, Routes,Link } from 'react-router-dom'

export class App extends Component {
  static propTypes = {

  }

  render() {
    return (
      <>
        <Navbar/>
        <Router>
          <Routes>
            <Route path="/general"><News pageSize={3} country={'us'} category={'general'}/></Route>
          </Routes>
        </Router>
      </>
    )
  }
}

export default App
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Engr Saddam Zardari
  • 1,057
  • 1
  • 14
  • 27
0

export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'

1st Method [replace Switch with Routes]

import { Routes , Route , Link } from 'react-router-dom';
<Routes>
 <Route exact path='/'>
  <HomePage />
 </Route>
</Routes>

2nd Method [downgrade your react-router-dom version to version 5 or below]

npm i react-router-dom@5.0.0