Skip to content

Commit 17a3625

Browse files
committed
Merge pull request #3444 from gaearon/pass-stuff-to-withrouter
Pass params and location as props in withRouter()
2 parents f64659d + 5aab7c9 commit 17a3625

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Given a route like `<Route path="/users/:userId" />`:
148148
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.
149149

150150
### `withRouter(component)`
151-
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.
151+
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.
152152

153153
### `<RouterContext>`
154154
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.

modules/__tests__/withRouter-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ describe('withRouter', function () {
1111
class App extends Component {
1212
render() {
1313
expect(this.props.router).toExist()
14+
expect(this.props.params).toExist()
15+
expect(this.props.params).toBe(this.props.router.params)
16+
expect(this.props.location).toExist()
17+
expect(this.props.location).toBe(this.props.router.location)
1418
return <h1>{this.props.router.location.pathname}</h1>
1519
}
1620
}
@@ -28,7 +32,7 @@ describe('withRouter', function () {
2832
unmountComponentAtNode(node)
2933
})
3034

31-
it('puts router on context', function (done) {
35+
it('puts router, props and location on context', function (done) {
3236
const WrappedApp = withRouter(App)
3337

3438
render((

modules/withRouter.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ function getDisplayName(WrappedComponent) {
1010
export default function withRouter(WrappedComponent) {
1111
const WithRouter = React.createClass({
1212
mixins: [ ContextSubscriber('router') ],
13+
1314
contextTypes: { router: routerShape },
15+
1416
render() {
15-
return <WrappedComponent {...this.props} router={this.context.router} />
17+
const { router } = this.context
18+
const { params, location } = router
19+
return (
20+
<WrappedComponent
21+
{...this.props}
22+
router={router}
23+
params={params}
24+
location={location}
25+
/>
26+
)
1627
}
1728
})
1829

0 commit comments

Comments
 (0)