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

Pass params and location as props in withRouter() #3444

Merged
merged 1 commit into from
May 9, 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
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