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

1
  • 1
    What version of react-router do you have? If you have v6 installed, you need to upgrade your code Commented Apr 19, 2022 at 15:10

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

npm install react-router-dom@5 solve my problem thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.