-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Throw more specific error if passed undefined in React.cloneElement #12534
Changes from 8 commits
7bc5fd5
8ee2452
33d1edf
48c7b53
34816fc
2733707
44df599
7f6d18b
baa4d84
80de944
e3416ee
905abfc
d20de72
070ef20
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 |
---|---|---|
|
@@ -12,6 +12,8 @@ import ReactCurrentOwner from './ReactCurrentOwner'; | |
|
||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
|
||
const invariant = require('fbjs/lib/invariant'); | ||
|
||
const RESERVED_PROPS = { | ||
key: true, | ||
ref: true, | ||
|
@@ -290,6 +292,11 @@ export function cloneAndReplaceKey(oldElement, newKey) { | |
* See https://reactjs.org/docs/react-api.html#cloneelement | ||
*/ | ||
export function cloneElement(element, config, children) { | ||
invariant( | ||
!(element === null || element === undefined), | ||
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 could just use a loose equality check here element != null 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. Ah right yes, because the type conversion will catch undefined 👍 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. FWIW, we've intentionally stopped using loose checks a while ago because they sometimes cause engine deoptimizations. I'd prefer strict ones. 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. @gaearon didn't know that. We still have tons of places where we use loose equality checks. 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. 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. @nicolevy I don't know, I'm not sure it's worth it? It could be helpful to make the changes and then somehow benchmark them. I'd also be interested in seeing a profile that shows the current deoptimizations and their impact as well. Making all the checks strict would increase the overall bundle size, so there are other costs to consider. 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. @aweary hmmm okay, I’ll wait until a decision is made before I start on that then. |
||
'Cannot clone a null or undefined element.' | ||
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. Maybe something like 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. Okay thanks that’s cool, I’m going to read up on how invariant works for the future for sure. I’m also going to look at writing some tests. Also wanted to ask if you have any input on this thread: #12534 (review) |
||
); | ||
|
||
let propName; | ||
|
||
// Original props are copied | ||
|
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 guess you could use ES6 imports instead since the former ones do?
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.
Yes please!