Search code examples
reactjsreact-dom

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


import './App.css';
import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

export default function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
          </ul>
        </nav>

        <Switch>
          <Route path="/login">
            <Login />
          </Route>
          <Route path="/reset">
            <PasswordReset />
          </Route>
          <Route path="/dashboard">
            <Dashboard />
          </Route>
          <Route path="/">
            <Home />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

function Home() {
  return <h2>Home</h2>;
}

Hi, I get an error. I am running the react application for the first time. 'Switch' (imported as 'Switch') was not found in 'react-router-dom' I tried typing Routers instead of Switch, it was not accepted. what can I do? thanks.


Solution

  • You should update 'react-router-dom' to v6 by 'npm -i --save react-router-dom@6'. Next, .. router v6 have new syntax, you should using Routes instead of Switch. Also, Routes consist of Route's, and use element instead of component, example:

    <Routes>
      <Route path='/'>
        <SomeComponent />
      </Route>
      
         or
    
      <Route path='/' element={<SomeComponent />} />
    </Routes>