-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Wrap render method created using class properties. #850
Conversation
if (render.__wrapped) { | ||
return render.__wrapped | ||
} | ||
|
||
const _render = function (...args) { | ||
try { | ||
return render.apply(this, args) | ||
return render.apply(context || this, args) |
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.
I wonder why we need context
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.
Yep. We don't need this.
} | ||
|
||
this.render = wrapRender(this.render, this) | ||
} |
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.
We should redefine constructor
instead of using componentDidMount
?
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.
I'm quite not sure about that. This constructor function is something generated by babel. I just used that to detect a React component.
originalComponentWillMount.apply(this, args) | ||
} | ||
|
||
this.render = wrapRender(this.render, this) |
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.
I think we should do this only when this.render
didn't come from prototype
.
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.
I think that's how it works. If there's a render method, it'll get wrapped by the above if block.
Did you mean something else?
@@ -19,6 +19,18 @@ export default (handleError = () => {}) => { | |||
const { prototype } = Component | |||
if (prototype && prototype.render) { | |||
prototype.render = wrapRender(prototype.render) | |||
} else if (prototype && prototype.constructor) { | |||
// Still a React component instance, but there's no render method in |
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.
btw there may be render
but might override it on constructor.
class Foo extends React.Component {
render () {}
}
class Bar extends Foo {
render = () => {};
}
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.
Since we do this on the componentWillMount
. I assume this is fine.
Isn't it?
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.
For now, we don't monkeypatch componentWillMount
if prototype.render
exists.
alternate PR #856 |
Fixes #828
With this, we are wrapping the render method added using ES2016+ class properties.
It doesn't add the render method to class's prototype. So, that's why we need to do this.