Skip to content
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

Merged
merged 8 commits into from
Aug 19, 2016

Conversation

Bigguy34
Copy link
Contributor

Following up on conversation from this bug. Adding the ability to specify an withRef option to be passed to withRouter. The signature has changed from withRouter(MyComponent) to withRouter()(MyComponent) except now you can do this withRouter({withRef:true})(MyComponent) and later instanceOfMyComponent.getWrappedInstance() returning the component instance wrapped by withRouter. This feature is also demonstrated in react-redux in code and is documented. If given the thumbs up I will update the documentation, and write a codemod script.

},
render() {
const router = this.props.router || this.context.router
if(options && options.withRef) {
Copy link
Contributor

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

render() {
const router = this.props.router || this.context.router
return <WrappedComponent {...this.props} router={router} />
if(options && options.withRef) {
Copy link
Contributor

Choose a reason for hiding this comment

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

persist this statically

Copy link
Contributor

Choose a reason for hiding this comment

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

code style

@timdorr
Copy link
Member

timdorr commented Aug 18, 2016

Just to address @taion's comments at a higher level, we'll be switching to a callback ref in the next version of react-redux. You can see that we set an instance variable instead of having a function call, and use a callback ref for setting it. That jives better with how React wants to handle refs in the future (i.e., removing string refs) and makes it a little easier to work with when you want to access that ref.


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.

@timdorr
Copy link
Member

timdorr commented Aug 19, 2016

LGTM. There's one small style change I want to make, but no need to go back and forth yet again on that. I'll clean it up after the fact.

@taion taion merged this pull request into remix-run:master Aug 19, 2016
timdorr pushed a commit that referenced this pull request Aug 19, 2016
#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
@taion
Copy link
Contributor

taion commented Aug 19, 2016

Oh, weird, how did this get double-merged? @timdorr

@timdorr
Copy link
Member

timdorr commented Aug 19, 2016

I squashed after the fact.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants