React-router-Link not working->On clicking The link The URL is updated as per but page is not rendered, I have already put exact in route

Viewed 370

the code goes as below,

    import './App.css';
    import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
    import Home from './Pages/Home';
    import About from './Pages/About';
    import Notfound from './Pages/Notfound';
    
    function App() {
      return (
        <Router>
          <div className="App">
            <nav>
              <ul>
                  <li><Link to="/" >Home</Link></li>
                  <li><Link to="/about">About</Link></li>                          
              </ul>
          </nav>
            <Switch>
              <Route exact path='/' component={Home} />
              <Route path='/about' component={About} />
              <Route component={Notfound} />
            </Switch>
          </div>
        </Router>
      );
    }
    export default App;
1 Answers

Few things needs to be fixed,

  1. Route should be defined inside Routes.
<Routes>
  <Route ... />
  ...
  ...
</Routes>
  1. Latest react-router-dom API has changed Route accepts component in the element prop not component.
<Route exact path="/" element={<Home />} />

Dependency versions

    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-router-dom": "6.3.0",
    "react-scripts": "4.0.0"

Edit cranky-goldberg-x06efn

Related