react-router-last-location
react-router-last-location copied to clipboard
preventLastLocation does not work correctly
Hi,
I have just installed lib into my project and I can't make preventLastLocation
working.
below is my render code:
return (
<NavLink
className={props.isActiveChild ? `${props.className} active` : props.className}
activeClassName="active"
to={{
pathname: `/${language}/${props.to}`,
state: {
preventLastLocation: true
},
}}
id={props.id}
>
{props.children}
</NavLink>
);
You can check it here that preventLastLocation
does not work
https://codesandbox.io/s/typescript-react-router-last-location-9xwoi
Versions:
"connected-react-router": "^6.3.1",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-router-last-location": "^2.0.1",
Hey,
After quick look it seems that it doesn't work because of HashRouter
which is a bit strange because it should work.
preventLastLocation
was introduced #17 to avoid storing "redirects" as the lastLocation
. Based on the provided demo, you want to exclude certain routes from storing as lastLocation
, right?
Thanks for reporting, I'll look into it.
exactly, I have just based tabs on react-router and above these tabs I want to have back button. My point is not to store lastLocation
of these tabs
@gitsad-mr Do you use HashRouter?
Yes, exactly
@gitsad-mr Hey, finally I've found some time to look into this issue. It has turned out that HashRouter
doesn't support location.key
and location.state
.
From the react-router
documentation
IMPORTANT NOTE: Hash history does not support location.key or location.state. In previous versions we attempted to shim the behavior but there were edge-cases we couldn’t solve. Any code or plugin that needs this behavior won’t work. As this technique is only intended to support legacy browsers, we encourage you to configure your server to work with <BrowserHistory> instead.
I will add extra prop to configure LastLocationProvider
in a such way that anyone will be able to block specific locations, something like: shouldLastLocationUpdate
prop.
You will be able then to write your own check, like:
const shouldLastLocationUpdate = (lastLocation, nextLocation) => {
// custom logic...
// you can ignore specific locations by returning `false` here
};
great! Good to hear it. I will check it 🎉