React Router change the address but doesn't load the second page

Viewed 40

i'm trying to load a new page on my react app, the address change correctly on the search bar but the page is not loading.

here my code:

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import Menu from "./components/Pages/Menu/index";
import { BrowserRouter, Route, Switch } from "react-router-dom";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <BrowserRouter>
    <Switch>
      <Route exact path="/" component={App} />
      <Route path="/Menu" component={Menu} />
    </Switch>
  </BrowserRouter>,
  rootElement
);

1 Answers

Are you sure you change the page correctly?

One of many methods:

<button onClick={() => window.location.replace("/Second")}>Change to Second page</button>

Should work for you.

Related