Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withRouter's match prop always returning the same path. #5947

Closed
enrique-ramirez opened this issue Feb 14, 2018 · 3 comments
Closed

withRouter's match prop always returning the same path. #5947

enrique-ramirez opened this issue Feb 14, 2018 · 3 comments

Comments

@enrique-ramirez
Copy link

Version

react-router-dom 4.2.2

Test Case

https://codesandbox.io/s/zw3okzz8ox

Steps to reproduce

Wrap a component with withRouter and then check the match prop.

Expected Behavior

props.match.path should update with the current path.

Actual Behavior

props.match.path always points to /.

Additional notes:

props.match.url behaves exactly the same: it always points to /. The only property that seems to change correctly is isExact, which is not of much use.

@enrique-ramirez
Copy link
Author

Woah, I seem to have messed thing up. withRouter seems to be meant to be inside a <Route> component or it won't work.

Now I have a question: I want to do something similar to the test case: have a global header that is aware of the current path.

I'm currently doing it pretty much like the test case, but match always returns /. Is there a better way to do this, other than connecting my header to the store?

@timdorr
Copy link
Member

timdorr commented Feb 14, 2018

The match isn't global or singular. There are potentially many within a component tree. It's relative to the nearest Route in your list of ancestors. withRouter just wraps your component with a pathless Route, so that's currently that route itself, which matches on the / part of the URL (which is always there).

@timdorr timdorr closed this as completed Feb 14, 2018
@enrique-ramirez
Copy link
Author

@timdorr thank you. I got it now.

Going to answer my own question in case someone stumbles upon this.

I created a layout. Basically, my routes now look like this:

function Layout(props) {
  const {
    component: Component,
    route,
  } = props

  return (
    <div>
      <Header />
      <Component route={route} />
      <Header />
    </div>
  )
}

function Routes() {
const layoutRender = component => route => <Layout component={component} route={route} />

return (
  <Switch>
    <Route
      path="/projects"
      render={layoutRender(Projects)}
    />
    <Route
      path="/"
      render={layoutRender(Splash)}
    />
  </Switch>
)

My <Header /> and <Footer /> components are functional components wrapped with withRouter, which now obtain the proper path.

Hope this helps someone!

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants