Using React Router withRouter

How do you get the routing context, location, parameters, etc. when using withRouter () ?

import { withRouter } from 'react-router'; const SomeComponent = ({location, route, params}) => ( <h1>The current location is {location.pathname}</h1> ); const ComposedWithRouter = withRouter(SomeComponent); 

Can you get this information using withRouter, or should these things be explicitly passed through the component tree?

+10
reactjs react-router
source share
2 answers

So, do not use context anymore. All this is available in the details:

 SomeComponent.propTypes = { location: React.PropTypes.shape({ pathname: React.PropTypes.string, query: React.PropTypes.shape({ ... }) }), params: React.PropTypes.shape({ ... }), router: React.PropTypes.object } const ComposedWithRouter = withRouter(SomeComponent); 

So then we say in SomeComponent that you want to send the user to a new route, just do this.props.router.push('someRoute')

+14
source share

Getting location, etc. via withRouter was added in Agent-router version 2.7. Dan Abramov recommends upgrading to 3.0 for use with the router. Prior to version 2.7, it provided only a set of functions.

+1
source share

All Articles