Skip to content

Commit e531f45

Browse files
committed
Some organization and style around withRouter ref.
1 parent af276b2 commit e531f45

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

modules/__tests__/withRouter-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('withRouter', function () {
6464
})
6565

6666
it('should support withRefs as a parameter', function (done) {
67-
const WrappedApp = withRouter(App, { withRef:true })
67+
const WrappedApp = withRouter(App, { withRef: true })
6868
const router = {
6969
push() {},
7070
replace() {},

modules/withRouter.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ function getDisplayName(WrappedComponent) {
99
}
1010

1111
export default function withRouter(WrappedComponent, options) {
12+
const { withRef } = options || {}
13+
1214
const WithRouter = React.createClass({
1315
contextTypes: { router: routerShape },
1416
propTypes: { router: routerShape },
17+
1518
getWrappedInstance() {
16-
warning(options && options.withRef, 'To access the wrappedInstance you must provide {withRef : true} as the second argument of the withRouter call')
17-
return this._wrappedComponent
19+
warning(withRef, 'To access the wrappedInstance you must provide { withRef: true } as the second argument of the withRouter call')
20+
return this.wrappedComponent
1821
},
22+
1923
render() {
20-
const router = this.props.router || this.context.router
21-
if (options && options.withRef) {
22-
return <WrappedComponent {...this.props} ref={(component)=>this._wrappedComponent = component} router={router} />
23-
} else {
24-
return <WrappedComponent {...this.props} router={router} />
25-
}
24+
const { router, ...props } = this.props
25+
26+
if (withRef) props.ref = component =>this.wrappedComponent = component
27+
28+
return <WrappedComponent {...props} router={router || this.context.router} />
2629
}
2730
})
2831

0 commit comments

Comments
 (0)