get previous route react router code example
Example 1: react router last page
import { useHistory } from "react-router-dom";
function demo () {
let history = useHistory();
const goToPreviousPath = () => {
history.goBack()
}
return (
<div>
<Button
onClick={goToPreviousPath}
>
Back
</Button>
</div>
):
}
Example 2: react history listen get previous location
let prevLocation;
history.listen(nextLocation => {
console.log(prevLocation);
// ...
prevLocation = nextLocation;
});