Skip to content
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
18 changes: 18 additions & 0 deletions modules/__tests__/withRouter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('withRouter', function () {
propTypes: {
router: routerShape.isRequired
}
testFunction() {
return 'hello from the test function'
}
render() {
expect(this.props.router).toExist()
return <h1>App</h1>
Expand Down Expand Up @@ -59,4 +62,19 @@ describe('withRouter', function () {

render(<Test router={router} test={test} />, node, done)
})

it('should support withRefs as a parameter', function (done) {
const WrappedApp = withRouter(App, { withRef:true })
const router = {
push() {},
replace() {},
go() {},
goBack() {},
goForward() {},
setRouteLeaveHook() {},
isActive() {}
}
const component = render((<WrappedApp router={router}/>), node, done)
expect(component.getWrappedInstance().testFunction()).toEqual('hello from the test function')
})
})
14 changes: 12 additions & 2 deletions modules/withRouter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import React from 'react'
import hoistStatics from 'hoist-non-react-statics'
import { routerShape } from './PropTypes'
import warning from './routerWarning'


function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}

export default function withRouter(WrappedComponent) {
export default function withRouter(WrappedComponent, options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would instead use destructuring here. It essentially makes the code self-documenting. So, { withRef } instead of options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd require doing e.g. { withRef: false } = {}, though. The transpiled code for default params is a bit ugly, plus there's that extra temporary object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? You can just do withRouter(WrappedComponent, { withRef = false }). But the default is undefined, so it's falsey and would work all the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, shit, you're right. Forgot about that.

const WithRouter = React.createClass({
contextTypes: { router: routerShape },
propTypes: { router: routerShape },
getWrappedInstance() {
warning(options && options.withRef, 'To access the wrappedInstance you must provide {withRef : true} as the second argument of the withRouter call')
return this._wrappedComponent
},
render() {
const router = this.props.router || this.context.router
return <WrappedComponent {...this.props} router={router} />
if (options && options.withRef) {
return <WrappedComponent {...this.props} ref={(component)=>this._wrappedComponent = component} router={router} />
} else {
return <WrappedComponent {...this.props} router={router} />
}
}
})

Expand Down