hinok / react-router-last-location

Provides access to the last location in react + react-router (v4.x, v5.x) applications. ❤️ Using hooks? useLastLocation | 💉 Using HOC? withLastLocation
MIT License
284 stars 24 forks source link

preventLastLocation does not work correctly #31

Open gitsad-mr opened 5 years ago

gitsad-mr commented 5 years ago

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",
hinok commented 5 years ago

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.

gitsad-mr commented 5 years ago

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

hinok commented 5 years ago

@gitsad-mr Do you use HashRouter?

gitsad-mr commented 5 years ago

Yes, exactly

hinok commented 5 years ago

@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 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
};
gitsad-mr commented 4 years ago

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