Skip to content

Commit 87d9b94

Browse files
jwbaytimdorr
authored andcommitted
add router to this.props for route components (#3486)
1 parent 2134f41 commit 87d9b94

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

docs/API.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ The dynamic segments of the URL.
485485
#### `route`
486486
The route that rendered this component.
487487

488+
#### `router`
489+
Contains methods relevant to routing. Most useful for imperatively transitioning around the application.
490+
488491
#### `routeParams`
489492
A subset of `this.props.params` that were directly specified in this component's route. For example, if the route's path is `users/:userId` and the URL is `/users/123/portfolios/345` then `this.props.routeParams` will be `{userId: '123'}`, and `this.props.params` will be `{userId: '123', portfolioId: 345}`.
490493

modules/RouterContext.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const RouterContext = React.createClass({
4444
},
4545

4646
render() {
47-
const { location, routes, params, components } = this.props
47+
const { location, routes, params, components, router } = this.props
4848
let element = null
4949

5050
if (components) {
@@ -58,6 +58,7 @@ const RouterContext = React.createClass({
5858
location,
5959
params,
6060
route,
61+
router,
6162
routeParams,
6263
routes
6364
}

modules/__tests__/RouterContext-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ describe('RouterContext', () => {
6767
done()
6868
})
6969
})
70+
71+
it('injects a `router` object into props of route components', (done) => {
72+
class RoutedComponent extends React.Component {
73+
render() {
74+
expect(this.props.router).toBeA(Object)
75+
return null
76+
}
77+
}
78+
79+
match({ location: '/', routes: { path: '/', component: RoutedComponent } }, (err, redirect, renderProps) => {
80+
render(<RouterContext {...renderProps} history={history} router={router} />, node, done)
81+
})
82+
})
7083

7184
describe('some weird tests that test implementation and should probably go away', () => {
7285
it('proxies calls to `push` to `props.history`', (done) => {

0 commit comments

Comments
 (0)