-
-
Notifications
You must be signed in to change notification settings - Fork 148
Only run proptypes checks on element creation like React. #370
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -270,10 +270,35 @@ function statelessComponentHook(Ctor) { | |
return Wrapped; | ||
} | ||
|
||
function validatePropTypes (vnode) { | ||
if (DEV) { | ||
let componentClass = typeof vnode.nodeName === "function" | ||
? vnode.nodeName | ||
: vnode.type; | ||
|
||
if (typeof componentClass !== "function") return; | ||
let name = componentClass.displayName || componentClass.name; | ||
let propTypes = componentClass.propTypes; | ||
if (propTypes) { | ||
let props = vnode.props; | ||
if ( | ||
!(vnode.props && vnode.props.children) && | ||
vnode.children && | ||
vnode.children.length | ||
) { | ||
props.children = vnode.children; | ||
} | ||
propsHook(props); | ||
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. Thought: does this need to be called even if 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. It is actually called all the time. see here. 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 I see what you mean now. 🤔 I don't think so. I could have sworn we fixed this a while back but I can't find where. 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. This is not an issue. We are rearranging the props so that prop-types detects the children correctly. |
||
PropTypes.checkPropTypes(propTypes, props, 'prop', name); | ||
} | ||
} | ||
} | ||
|
||
function createElement(...args) { | ||
upgradeToVNodes(args, 2); | ||
return normalizeVNode(h(...args)); | ||
let node = h(...args); | ||
validatePropTypes(node); | ||
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. Just had a thought (I think I was noodling on this when I failed to review before): What if we moved the invocation of this validation into 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 honestly got this working in a very specific way and I don't remember all the details. I do remember that I had good reasons for doing it the way I did though 😅 |
||
return normalizeVNode(node); | ||
} | ||
|
||
|
||
|
@@ -293,7 +318,6 @@ function normalizeVNode(vnode) { | |
} | ||
|
||
applyEventNormalization(vnode); | ||
|
||
return vnode; | ||
} | ||
|
||
|
@@ -315,10 +339,11 @@ function cloneElement(element, props, ...children) { | |
else if (props && props.children) { | ||
cloneArgs.push(props.children); | ||
} | ||
return normalizeVNode(preactCloneElement(...cloneArgs)); | ||
let newNode = preactCloneElement(...cloneArgs); | ||
validatePropTypes(newNode); | ||
return normalizeVNode(newNode); | ||
} | ||
|
||
|
||
function isValidElement(element) { | ||
return element && ((element instanceof VNode) || element.$$typeof===REACT_ELEMENT_TYPE); | ||
} | ||
|
@@ -489,7 +514,7 @@ function multihook(hooks, skipDuplicates) { | |
|
||
|
||
function newComponentHook(props, context) { | ||
propsHook.call(this, props, context); | ||
propsHook(props, context); | ||
this.componentWillReceiveProps = multihook([propsHook, this.componentWillReceiveProps || 'componentWillReceiveProps']); | ||
this.render = multihook([propsHook, beforeRender, this.render || 'render', afterRender]); | ||
} | ||
|
@@ -509,20 +534,8 @@ function propsHook(props, context) { | |
props.children[0] = props.children; | ||
} | ||
} | ||
|
||
// add proptype checking | ||
if (DEV) { | ||
let ctor = typeof this==='function' ? this : this.constructor, | ||
propTypes = this.propTypes || ctor.propTypes; | ||
const displayName = this.displayName || ctor.name; | ||
|
||
if (propTypes) { | ||
PropTypes.checkPropTypes(propTypes, props, 'prop', displayName); | ||
} | ||
} | ||
} | ||
|
||
|
||
function beforeRender(props) { | ||
currentComponent = 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 this can just always look at
vnode.nodeName
, right?