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

useLocation returns incorrect object

Open bibblebabl opened this issue 5 years ago • 2 comments

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 using Router with 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 that location.pathname is undefined. Very strange.

Hence I ended up adding the useLocation hook above to examine the context's location object.

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 useLocation looks like this:

{action: "PUSH", location: {…}}

Which explains why location.pathname is undefined, and why my code breaks.

This object looks like a subset of the history object, rather than the location object 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):

  1. I start with /, everything looks good:

image

  1. I click a link, and I see this:

image

  1. And then immediately after:

image

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 BrowserRouter and I click on a link:

image

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: {}

bibblebabl avatar Dec 20 '20 21:12 bibblebabl

Seems like history v5 is not playing well with React Router. Downgrade to v4 will solve the issue.

idangozlan avatar Dec 21 '20 09:12 idangozlan

history v5 is for react router v6

pshrmn avatar Dec 21 '20 17:12 pshrmn