You can save previous path in a componentWillReceiveProps lifecycle method. The logic is very close to the example provided in troubleshooting section of react-router docs.
Instead of checking what the previous page is, approach the problem from a different angle. Pass the current page as props to the component or link that you're going to navigate to.
In the previous page or component that I'm calling history.push or clicking the link from, I add a state of the current page that I'm on e.g.
Using context you can store the previous location pathname:
const RouterContext = React.createContext();
const RouterProvider = ({children}) => {
const location = useLocation()
const [route, setRoute] = useState({ //--> It can be replaced with useRef or localStorage
to: location.pathname,
from: location.pathname //--> previous pathname
});
useEffect(()=> {
setRoute((prev)=> ({to: location.pathname, from: prev.to}) )
}, [location]);
return <RouterContext.Provider value={route}>
{children}
</RouterContext.Provider>
}
Then in some component under RouterProvider:
const route = useContext(RouterContext);
//...
<Link to={route.from}>
Go Back
</Link>
Or
history.push(route.from);
Note: RouterContext should be under Router component and If you don't want to update the state you can use useRef instead. If you need more persistence use localStorage
This answer uses a similar approach to @AlexandrLazarev, but implements it via React Hooks. This ensures that all changes to the path are captured regardless of how they are initiated. The previous path value is stored in the top level component's state which can then be passed down to children as props or if you're using a global state provider like Redux can be added to a store:
The implementation in the markup would look something like the below snippet. I've been using Reach Router, but given that its been merged with React Router it should work there as well. The Router component makes the location prop available to all of its children and holds the value of the current path under its pathname attribute
I needed a way to conditionally navigate only if previous path equals a specific route. With a functional component it worked out like this. The && will fire the .push() method only if route is '/cart'.
import {useHistory} from "react-router-dom";
const history = useHistory();
history.location.pathname === '/cart' && history.push('/checkout');
As of 08/2022, for those using React Router v6, you can either use Navigate component, Link component or useNavigate hook to pass the previous url to the next component:
In the redirecting component:
// with Navigate component (same as with Link component):
const location = useLocation();
...
<Navigate to="/nextpath" state={ { from: location } } />
...
// with useNavigate hook:
const navigate = useNavigate();
const location = useLocation();
....
navigate("/nextpath", { state: { from: location } });
...
In the component you redirected to:
...
const location = useLocation();
let from = location.state?.from?.pathname;
...