react-router-last-location icon indicating copy to clipboard operation
react-router-last-location copied to clipboard

preventLastLocation does not work correctly

Open gitsad-mr opened this issue 5 years ago • 6 comments

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",

gitsad-mr avatar Jul 22 '19 11:07 gitsad-mr

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.

hinok avatar Jul 22 '19 14:07 hinok

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 avatar Jul 22 '19 15:07 gitsad-mr

@gitsad-mr Do you use HashRouter?

hinok avatar Jul 22 '19 15:07 hinok

Yes, exactly

gitsad-mr avatar Jul 22 '19 16:07 gitsad-mr

@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
};

hinok avatar Sep 30 '19 18:09 hinok

great! Good to hear it. I will check it 🎉

gitsad-mr avatar Oct 07 '19 08:10 gitsad-mr