-
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -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 | ||
// prototype. This happens when the render method created with class-properties. | ||
// With this fix, we'll wrap the render method in runtime when the component initialized | ||
const originalComponentWillMount = prototype.componentWillMount | ||
prototype.componentWillMount = function (...args) { | ||
if (originalComponentWillMount) { | ||
originalComponentWillMount.apply(this, args) | ||
} | ||
|
||
this.render = wrapRender(this.render, this) | ||
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 think we should do this only when 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 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? |
||
} | ||
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. We should redefine 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'm quite not sure about that. This constructor function is something generated by babel. I just used that to detect a React component. |
||
} else { | ||
// stateless component | ||
Component = wrapRender(Component) | ||
|
@@ -38,14 +50,14 @@ export default (handleError = () => {}) => { | |
return forceUpdate.apply(this, args) | ||
} | ||
|
||
function wrapRender (render) { | ||
function wrapRender (render, context) { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I wonder why we need 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. Yep. We don't need this. |
||
} catch (err) { | ||
handleError(err) | ||
return null | ||
|
@@ -54,7 +66,6 @@ export default (handleError = () => {}) => { | |
|
||
// copy all properties | ||
Object.assign(_render, render) | ||
|
||
render.__wrapped = _render.__wrapped = _render | ||
|
||
return _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.
btw there may be
render
but might override it on constructor.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
ifprototype.render
exists.