-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Don't define key or ref dummy props if none were provided #7488
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,50 @@ function hasValidKey(config) { | |
return config.key !== undefined; | ||
} | ||
|
||
function defineKeyPropWarningGetter(props, displayName) { | ||
var warnAboutAccessingKey = function() { | ||
if (!specialPropKeyWarningShown) { | ||
specialPropKeyWarningShown = true; | ||
warning( | ||
false, | ||
'%s: `key` is not a prop. Trying to access it will result ' + | ||
'in `undefined` being returned. If you need to access the same ' + | ||
'value within the child component, you should pass it as a different ' + | ||
'prop. (https://fb.me/react-special-props)', | ||
displayName | ||
); | ||
} | ||
return undefined; | ||
}; | ||
warnAboutAccessingKey.isReactWarning = true; | ||
Object.defineProperty(props, 'key', { | ||
get: warnAboutAccessingKey, | ||
configurable: true, | ||
}); | ||
} | ||
|
||
function defineRefPropWarningGetter(props, displayName) { | ||
var warnAboutAccessingRef = function() { | ||
if (!specialPropRefWarningShown) { | ||
specialPropRefWarningShown = true; | ||
warning( | ||
false, | ||
'%s: `ref` is not a prop. Trying to access it will result ' + | ||
'in `undefined` being returned. If you need to access the same ' + | ||
'value within the child component, you should pass it as a different ' + | ||
'prop. (https://fb.me/react-special-props)', | ||
displayName | ||
); | ||
} | ||
return 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. (same here) |
||
}; | ||
warnAboutAccessingRef.isReactWarning = true; | ||
Object.defineProperty(props, 'ref', { | ||
get: warnAboutAccessingRef, | ||
configurable: true, | ||
}); | ||
} | ||
|
||
/** | ||
* Factory method to create a new React element. This no longer adheres to | ||
* the class pattern, so do not use new to call it. Also, no instanceof check | ||
|
@@ -209,56 +253,18 @@ ReactElement.createElement = function(type, config, children) { | |
} | ||
} | ||
if (__DEV__) { | ||
var displayName = typeof type === 'function' ? | ||
(type.displayName || type.name || 'Unknown') : | ||
type; | ||
|
||
// Create dummy `key` and `ref` property to `props` to warn users against its use | ||
var warnAboutAccessingKey = function() { | ||
if (!specialPropKeyWarningShown) { | ||
specialPropKeyWarningShown = true; | ||
warning( | ||
false, | ||
'%s: `key` is not a prop. Trying to access it will result ' + | ||
'in `undefined` being returned. If you need to access the same ' + | ||
'value within the child component, you should pass it as a different ' + | ||
'prop. (https://fb.me/react-special-props)', | ||
displayName | ||
); | ||
} | ||
return undefined; | ||
}; | ||
warnAboutAccessingKey.isReactWarning = true; | ||
|
||
var warnAboutAccessingRef = function() { | ||
if (!specialPropRefWarningShown) { | ||
specialPropRefWarningShown = true; | ||
warning( | ||
false, | ||
'%s: `ref` is not a prop. Trying to access it will result ' + | ||
'in `undefined` being returned. If you need to access the same ' + | ||
'value within the child component, you should pass it as a different ' + | ||
'prop. (https://fb.me/react-special-props)', | ||
displayName | ||
); | ||
} | ||
return undefined; | ||
}; | ||
warnAboutAccessingRef.isReactWarning = true; | ||
|
||
if (typeof props.$$typeof === 'undefined' || | ||
props.$$typeof !== REACT_ELEMENT_TYPE) { | ||
if (!props.hasOwnProperty('key')) { | ||
Object.defineProperty(props, 'key', { | ||
get: warnAboutAccessingKey, | ||
configurable: true, | ||
}); | ||
} | ||
if (!props.hasOwnProperty('ref')) { | ||
Object.defineProperty(props, 'ref', { | ||
get: warnAboutAccessingRef, | ||
configurable: true, | ||
}); | ||
if (key || ref) { | ||
if (typeof props.$$typeof === 'undefined' || | ||
props.$$typeof !== REACT_ELEMENT_TYPE) { | ||
var displayName = typeof type === 'function' ? | ||
(type.displayName || type.name || 'Unknown') : | ||
type; | ||
if (key) { | ||
defineKeyPropWarningGetter(props, displayName); | ||
} | ||
if (ref) { | ||
defineRefPropWarningGetter(props, displayName); | ||
} | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is it useful to
return undefined;
as the last line of a function?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.
Copypasta. Not sure why it was there. Removing.