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

1

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

Zoochore answered 19/4, 2022 at 15:0 Comment(1)
What version of react-router do you have? If you have v6 installed, you need to upgrade your codeOklahoma
F
6

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>
Forebrain answered 19/4, 2022 at 19:20 Comment(1)
npm install react-router-dom@5 solve my problem thanksZoochore

© 2022 - 2024 — McMap. All rights reserved.