mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Don't define key or ref dummy props if none were provided (#7488)
* Don't define key or ref dummy props if none were provided This fixes a performance regression. * Style nit
This commit is contained in:
@@ -52,6 +52,48 @@ 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
|
||||
);
|
||||
}
|
||||
};
|
||||
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
|
||||
);
|
||||
}
|
||||
};
|
||||
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 +251,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user