Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add router to props for route components #3486

Merged
merged 1 commit into from
May 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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