Skip to content

Commit

Permalink
Merge pull request #3444 from gaearon/pass-stuff-to-withrouter
Browse files Browse the repository at this point in the history
Pass params and location as props in withRouter()
  • Loading branch information
gaearon committed May 9, 2016
2 parents f64659d + 5aab7c9 commit 17a3625
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Given a route like `<Route path="/users/:userId" />`:
An `<IndexLink>` is like a [`<Link>`](#link), except it is only active when the current route is exactly the linked route. It is equivalent to `<Link>` with the `onlyActiveOnIndex` prop set.

### `withRouter(component)`
A HoC (higher-order component) that wraps another component to provide `this.props.router`. Pass in your component and it will return the wrapped component.
A HoC (higher-order component) that wraps another component to provide `this.props.router`, `this.props.params`, and `this.props.location`. Pass in your component and it will return the wrapped component.

### `<RouterContext>`
A `<RouterContext>` renders the component tree for a given router state. Its used by `<Router>` but also useful for server rendering and integrating in brownfield development.
Expand Down
6 changes: 5 additions & 1 deletion modules/__tests__/withRouter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('withRouter', function () {
class App extends Component {
render() {
expect(this.props.router).toExist()
expect(this.props.params).toExist()
expect(this.props.params).toBe(this.props.router.params)
expect(this.props.location).toExist()
expect(this.props.location).toBe(this.props.router.location)
return <h1>{this.props.router.location.pathname}</h1>
}
}
Expand All @@ -28,7 +32,7 @@ describe('withRouter', function () {
unmountComponentAtNode(node)
})

it('puts router on context', function (done) {
it('puts router, props and location on context', function (done) {
const WrappedApp = withRouter(App)

render((
Expand Down
13 changes: 12 additions & 1 deletion modules/withRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ function getDisplayName(WrappedComponent) {
export default function withRouter(WrappedComponent) {
const WithRouter = React.createClass({
mixins: [ ContextSubscriber('router') ],

contextTypes: { router: routerShape },

render() {
return <WrappedComponent {...this.props} router={this.context.router} />
const { router } = this.context
const { params, location } = router
return (
<WrappedComponent
{...this.props}
router={router}
params={params}
location={location}
/>
)
}
})

Expand Down

0 comments on commit 17a3625

Please sign in to comment.