I'm having trouble detecting route changes with React Router v5. The desire is, when the route changes I want output a console.log. The desire is, when the route changes I want output that route in a console.log in the App component (as shown in the code below) so I can then set a variable in App.js based on the route.
Package versions:
"dependencies": {
"react": "^16.12.0",
"react-router-dom": "^5.1.2",
}
This is the App.js code where my router is currently located:
import React from "react";
import { BrowserRouter as Router, Route, Switch, NavLink } from "react-router-dom"
import "./App.css";
import AdminDashboard from "./pages/AdminDashboard"
import UserList from "./pages/UserList"
function App() {
const routeChange = () => {
console.log("foo!")
console.log(window.location.pathname)
}
return (
<>
<Router onChange={routeChange}>
<NavLink exact={true} className="nav-link" to="/AdminDashboard">Admin Dashboard</NavLink>
<NavLink exact={true} className="nav-link" to="/UserList">User List</NavLink>
<Switch>
<Route path="/AdminDashboard" component={AdminDashboard} exact />
<Route path="/UserList" component={UserList} exact />
</Switch>
</Router>
</>
);
}
export default App;
onChange
event on theRouter
tag it's pointless. – Mohamed Wagih