Routes not working in react router

14603


I have 2 routes, But only the first route is working. The other route shows blank screen, I have my App.js like below. How to fix?

import React from "react";
import Nav from "./components/nav";
import Home from "./components/Home";
import { BrowserRouter, Route, Switch } from "react-router-dom"

const App = () => {
    return (
        <BrowserRouter>
            <Switch>
                <Route path="/" component={Nav} />
                <Route path="/home" component={Home} />
            </Switch>
        </BrowserRouter>
    );
};

export default App;

1 Answer

4 years ago by

The problem is with the Nav Route, It is the base route to all routes. So, you need to tell react router that there are other routes appending that route. So, you need to add exact in the Route like below

<Route exact path="/" component={Nav} />

4 years ago by Divya