-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Adding support to get the component instance that withRouter HOC wraps #3735
Changes from 3 commits
048be89
6d861e7
4bdccb0
9ec7563
ae24bcf
017eb4b
e9eb174
f482d2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,40 @@ | ||
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(options) { | ||
const wrapWithRouter = function (WrappedComponent) { | ||
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 first argument of the withRouter call') | ||
return this.refs.wrappedInstance | ||
}, | ||
render() { | ||
const router = this.props.router || this.context.router | ||
if(options && options.withRef) { | ||
return <WrappedComponent {...this.props} ref="wrappedInstance" router={router} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use a function ref |
||
}else{ | ||
return <WrappedComponent {...this.props} router={router} /> | ||
} | ||
} | ||
}) | ||
WithRouter.displayName = `withRouter(${getDisplayName(WrappedComponent)})` | ||
WithRouter.WrappedComponent = WrappedComponent | ||
|
||
export default function withRouter(WrappedComponent) { | ||
const WithRouter = React.createClass({ | ||
contextTypes: { router: routerShape }, | ||
propTypes: { router: routerShape }, | ||
render() { | ||
const router = this.props.router || this.context.router | ||
return <WrappedComponent {...this.props} router={router} /> | ||
} | ||
}) | ||
return hoistStatics(WithRouter, WrappedComponent) | ||
} | ||
|
||
WithRouter.displayName = `withRouter(${getDisplayName(WrappedComponent)})` | ||
WithRouter.WrappedComponent = WrappedComponent | ||
|
||
return hoistStatics(WithRouter, WrappedComponent) | ||
if(typeof(options) === 'function') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? Just make |
||
warning(false,'passing a component to the first invocation of withRouter has been depreciated, withRouter'+ | ||
'now needs to be invoked twice, once to pass the options through, and a second time with the component eg. withRouter()(MyComponent) or withRouter({withRef:true})(MyComponent)') | ||
return wrapWithRouter(options) | ||
}else{ | ||
return wrapWithRouter | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please match the code style elsewhere in the project