When using a HashRouter, and within a component using useLocation, there is a discrepancy between the window.location object and the location object from useLocation.
As I am writing this I have noticed there may be a confusion around what useLocation is actually pointing to. The pathname from useLocation seems to actually be the window.location.hash without the #. But the hash seems to not be pointing at anything at all.
Honestly I wasn't sure if I should've posted this as an issue on react-routers Github, but I wanted to post this here in case there is some fundamental piece of info I'm missing about how useLocation is supposed to work (more importantly in relation to what router you are using).
Does useLocation behave differently when you wrap it in <BrowserRouter> vs <HashRouter> vs <MemoryRouter> etc.?
Values from window.location:
{
pathname: "/myPage/"
hash: "#/testpath/123"
search: ""
}
Values from location = useLocation():
{
pathname: "/testpath/123"
hash: ""
search: ""
key: "abc123"
state: null
}
The
HashRouterrouter uses the path defined after the first hash of the URL.For example, given:
<Route path="/testpath/123" element={....} />"https://..../#/testpath/123"The
locationreturned from theuseLocationhook may look like:As compared to
window.locationGiven:
<Route path="/testpath/123" element={....} />"https://..../#/testpath/123#foo"Then the
locationobject would look like:Notice the only difference now is that the
"foo"is now part of thelocationobject, the "hash" of the route path the app's router is using. It also is included in the hash ofwindow.location, e.g. everything after the first hash.No, not really. These routers all manage and/or maintain their own internal history object and state, e.g. what
locationis in each context. TheBrowserRoutercouples internal routing/navigation actions to a browser's DOM, and similarly theHashRouteruses just the hash of what is effectively a static location, e.g. an app hosted/served from a specific directory on a server. TheMemoryRouteris intended to be used in non-DOM environments, e.g. Node environments like Jest testing, etc.Depending on which router you use, and what the URL path/etc value is, some of the
locationproperties may not be set, but theuseLocationhook andlocationobject is basically the same regardless.I don't believe there is a bug or issue to file here as everything you described seems like reasonable behavior given the
HashRouterand the console logging you used.