Skip to content

Commit

Permalink
add router to this.props for route components (#3486)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay authored and timdorr committed May 31, 2016
1 parent 2134f41 commit 87d9b94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ The dynamic segments of the URL.
#### `route`
The route that rendered this component.

#### `router`
Contains methods relevant to routing. Most useful for imperatively transitioning around the application.

#### `routeParams`
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}`.

Expand Down
3 changes: 2 additions & 1 deletion modules/RouterContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const RouterContext = React.createClass({
},

render() {
const { location, routes, params, components } = this.props
const { location, routes, params, components, router } = this.props
let element = null

if (components) {
Expand All @@ -58,6 +58,7 @@ const RouterContext = React.createClass({
location,
params,
route,
router,
routeParams,
routes
}
Expand Down
13 changes: 13 additions & 0 deletions modules/__tests__/RouterContext-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ describe('RouterContext', () => {
done()
})
})

it('injects a `router` object into props of route components', (done) => {
class RoutedComponent extends React.Component {
render() {
expect(this.props.router).toBeA(Object)
return null
}
}

match({ location: '/', routes: { path: '/', component: RoutedComponent } }, (err, redirect, renderProps) => {
render(<RouterContext {...renderProps} history={history} router={router} />, node, done)
})
})

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

0 comments on commit 87d9b94

Please sign in to comment.