-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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 6 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,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) { | ||
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. I would instead use destructuring here. It essentially makes the code self-documenting. So, 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. That'd require doing e.g. 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. Huh? You can just do 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. That doesn't work unless the caller explicitly passes in an object as the 2nd arg: http://babeljs.io/repl/#?evaluate=true&lineWrap=false&presets=es2015-loose&experimental=true&loose=true&spec=false&code=function%20foo(%7B%20withRef%20%7D)%20%7B%0A%20%20console.log(withRef)%3B%0A%7D%0A%0Afoo()%3B&playground=false 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. 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.refs.wrappedInstance | ||
}, | ||
render() { | ||
const router = this.props.router || this.context.router | ||
return <WrappedComponent {...this.props} router={router} /> | ||
if(options && options.withRef) { | ||
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. persist this statically 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. code style |
||
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 function ref |
||
}else{ | ||
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. code style |
||
return <WrappedComponent {...this.props} router={router} /> | ||
} | ||
} | ||
}) | ||
|
||
|
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.
code style