Skip to content

Commit af276b2

Browse files
Bigguy34timdorr
authored andcommitted
Adding support to get the component instance that withRouter HOC wraps (#3735)
* More code style fixes * Fixing code style, and updating ref to be a function ref * updating error message from not passing in proper options object * Updating withRouter based on comments * Reverting change to withRouter * Last minute spacing issues * Fixing some spacing issues * withRouter now supports withRef as an option, to better support getting the wrapped instance
1 parent af58641 commit af276b2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

modules/__tests__/withRouter-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ describe('withRouter', function () {
1212
propTypes: {
1313
router: routerShape.isRequired
1414
}
15+
testFunction() {
16+
return 'hello from the test function'
17+
}
1518
render() {
1619
expect(this.props.router).toExist()
1720
return <h1>App</h1>
@@ -59,4 +62,19 @@ describe('withRouter', function () {
5962

6063
render(<Test router={router} test={test} />, node, done)
6164
})
65+
66+
it('should support withRefs as a parameter', function (done) {
67+
const WrappedApp = withRouter(App, { withRef:true })
68+
const router = {
69+
push() {},
70+
replace() {},
71+
go() {},
72+
goBack() {},
73+
goForward() {},
74+
setRouteLeaveHook() {},
75+
isActive() {}
76+
}
77+
const component = render((<WrappedApp router={router}/>), node, done)
78+
expect(component.getWrappedInstance().testFunction()).toEqual('hello from the test function')
79+
})
6280
})

modules/withRouter.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import React from 'react'
22
import hoistStatics from 'hoist-non-react-statics'
33
import { routerShape } from './PropTypes'
4+
import warning from './routerWarning'
5+
46

57
function getDisplayName(WrappedComponent) {
68
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
79
}
810

9-
export default function withRouter(WrappedComponent) {
11+
export default function withRouter(WrappedComponent, options) {
1012
const WithRouter = React.createClass({
1113
contextTypes: { router: routerShape },
1214
propTypes: { router: routerShape },
15+
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
18+
},
1319
render() {
1420
const router = this.props.router || this.context.router
15-
return <WrappedComponent {...this.props} router={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+
}
1626
}
1727
})
1828

0 commit comments

Comments
 (0)