-
-
Notifications
You must be signed in to change notification settings - Fork 785
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
Use explicit constructor in React components #26
Conversation
@@ -114,7 +114,7 @@ export default function createListComponent({ | |||
validateProps: ValidateProps, | |||
|}) { | |||
return class List extends PureComponent<Props, State> { | |||
_instanceProps: any = initInstanceProps(this.props, this); | |||
_instanceProps: any; |
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.
Was this a necessary part of the change as well?
I like the property initializer syntax.
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.
output with "empty" constructor (just super call)
var List3 =
/*#__PURE__*/
function (_PureComponent3) {
_inheritsLoose(List3, _PureComponent3);
function List3(props) {
var _this3;
_this3 = _PureComponent3.call(this, props) || this;
_this3._instanceProps = initInstanceProps(_this3.props, _assertThisInitialized(_this3));
return _this3;
}
return List3;
}(PureComponent);
I'm testing on babel@7 but babel@6 should produce similar code in this regard.
Let's also focus only on the actual part that differ
With property initializer
_this._instanceProps = initInstanceProps(_this.props, _assertThisInitialized(_this));
Without property initializer
_this._instanceProps = initInstanceProps(props, _assertThisInitialized(_this));
So the difference is really negligible (but its there 🤣 ). Manual initializing can leverage props
from the arguments and doesn't have to reach for it to the this
, so it's shorter by few characters (assume mangled variable name for props, like a
against mangled this reference b.props
)
Can adjust it either way - this difference is micro even for me 😄 Before the PR they were also not unified, so this PR actually unified style around this - just maybe not in the way you'd like to. Let me know which version do you chose :)
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.
This change doesn't actually seem necessary 😄 Good.
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.
😆 Looks like our comments overlapped.
Yeah, I had checked out your branch locally and noticed the inconsistency.
I'm not sure I'm ready to care about the size of an extra "this" keyword here and there. (My variable and class names are pretty big after all.) Stuff like that becomes less important once the lib is used in an actual app and uglified.
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.
Sure thing, as I've said - this one is super micro so I dont care about it that much either. Was just commenting what's the difference because at first I chose to include this initializer manually in the constructor. I guess mainly because this:
constructor(props) {
super(props)
}
looks super weird :p
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.
Agreed! 😁 It also triggers a "useless constructor" lint warning hehe.
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.
This is neat 😄 I learned something new.
FYI this was just published as part of 1.1.0 Thanks again~ |
cool! thanks for working on useful libs in your free time! ❤️ |
This boils down to a fact that
super
has to be called with all arguments if the constructor is not specified (when we use explicit constructor, we explicitly callsuper(props)
), so arguments have to be looped and super constructor has to be applied with them.Implicit constructor
Explicit one