I am trying to get the url of the previous page I came from in react.
So if I came from xxx.com/register
, and am now on ...com/profile
, then how do I log that in the console?
I have no clue where to start. Does anyone know how to do this? Is this possible in react
? Should I use js
instead?
We can access the previous page using useHistory()
import { useHistory } from "react-router-dom";
//...
const history = useHistory();
//...
<div onClick={() => { history.push( 'pages/previous-page', { from: "previous-page" }) }>
Previous Page
</div>
On next page you can check by this code
console.log("Landed on this page from ", history.location.state.from )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With