Below is a snippet from my app.js cra file, where I am using react-router-dom to navigate to various pages, the problem is, for /payment route, entering the url to payment: localhost:3000/checkout returns payment page component instead, useHistory with onClick on the header component also does the same, no error is thrown, I have already gone through the app twice searching for typos or any such fault.
The app.js snippet
import "./App.css";
import Home from "./Home";
import Checkout from "./Checkout";
import Login from "./Login";
import Payment from "./Payment";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
function App() {
const [state, dispatch] = useStateValue();
return (
<Router>
<div className="app">
<Switch>
<Route path="/login">
<Login />
</Route>
<Route path="/checkout">
<Checkout />
</Route>
<Route path="/payment">
<Payment />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}
the useHistory inside of a component:
import { useHistory } from "react-router-dom";
export default function Subtotal() {
const [state, dispatch] = useStateValue();
const history = useHistory();
return (
<div className="subtotal">
<button onClick={history.push("/payment")}>Proceed to checkout</button>
</div>
);
}
Quick note: everything works fine until I introduce a new route to the already existing ones