useLocation returns incorrect object
Version
"react-router-dom": "^5.2.0", "history": "^5.0.0",The problem
I've struggling with a really strange problem. My top level render tree looks like this:
import { Switch, Route, Router, Redirect, useHistory, useLocation } from 'react-router-dom'; import { createBrowserHistory } from 'history'; ... const history = createBrowserHistory(); ... And eventually I hydrate with... const ClientApp = () => ( <Router history={history}> <ApolloProvider client={apolloClient}> <ThemeProvider theme={theme}> <ThemeProvider theme={{ props: { MuiUseMediaQuery: { ssrMatchMedia } } }}> <UserProvider render={ ({ me: curUser, loading: loadingCurUser, refetch: refetchSelf }) => { const history = useHistory(); const location = useLocation(); console.log(history, location); return ( <Switch> <! -- Snipped Routes -> ... <Route render={({ location }) => <Redirect to={`/${defaultLocale}${location.pathname}`} />} /> </Switch> ); } } /> </ThemeProvider> </ThemeProvider> </ApolloProvider> </Router> );Before I continue, I'd like to state that if I use
<BrowserRouter>instead of<Router history={history}>then everything works perfectly. The problem I'm about to describe only happens when usingRouterwith my history object, which appears to me to be exactly what<BrowserRouter>is doing under the hood anyway...The problem that I initially observed when when moving from
<BrowserRouter>to<Router history={history}>was that none of my Routes matched (except for/), and I fell to the last route (seen code above) which tried to redirect to/enundefined, which implied thatlocation.pathnameis undefined. Very strange.Hence I ended up adding the
useLocationhook above to examine the context'slocationobject.Now, if I render the home page my debug prints the expected result:
Object {pathname: "/", search: "", hash: "", state: null, key: "default"}However, if I click on any link at all then the location object returned by
useLocationlooks like this:{action: "PUSH", location: {…}}Which explains why
location.pathnameis undefined, and why my code breaks.This object looks like a subset of the
historyobject, rather than thelocationobject itself. Here are a series of screenshots of the console as I click on a link (in each case, history on left, location on right):
- I start with
/, everything looks good:
- I click a link, and I see this:
- And then immediately after:
I'm at a complete loss. Examining the source code to BrowserRouter suggest that I'm doing the same thing that it does.
Just of the sake of completeness, if I use
BrowserRouterand I click on a link:
Still having this issue.
"react-router-dom": "^5.2.0",
"history": "^5.0.0",
import { createBrowserHistory } from 'history'
const history = createBrowserHistory()
<Router history={history}
<App />
</Router>
after history.push() useLocation returns
action: "PUSH"
location: {}
Seems like history v5 is not playing well with React Router. Downgrade to v4 will solve the issue.
history v5 is for react router v6



