mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #3275 from spicyj/gh-3222
Don't use undefined as parent name in key warning
This commit is contained in:
@@ -134,7 +134,8 @@ function validatePropertyKey(name, element, parentType) {
|
||||
*/
|
||||
function warnAndMonitorForKeyUse(message, element, parentType) {
|
||||
var ownerName = getCurrentOwnerDisplayName();
|
||||
var parentName = parentType.displayName || parentType.name;
|
||||
var parentName = typeof parentType === 'string' ?
|
||||
parentType : parentType.displayName || parentType.name;
|
||||
|
||||
var useName = ownerName || parentName;
|
||||
var memoizer = ownerHasKeyUseWarning[message] || (
|
||||
@@ -145,9 +146,10 @@ function warnAndMonitorForKeyUse(message, element, parentType) {
|
||||
}
|
||||
memoizer[useName] = true;
|
||||
|
||||
message += ownerName ?
|
||||
` Check the render method of ${ownerName}.` :
|
||||
` Check the React.render call using <${parentName}>.`;
|
||||
message +=
|
||||
ownerName ? ` Check the render method of ${ownerName}.` :
|
||||
parentName ? ` Check the React.render call using <${parentName}>.` :
|
||||
'';
|
||||
|
||||
// Usually the current owner is the offender, but if it accepts children as a
|
||||
// property, it may be the creator of the child that's responsible for
|
||||
|
||||
@@ -80,6 +80,46 @@ describe('ReactElementValidator', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('warns for keys for arrays with no owner or parent info', function() {
|
||||
spyOn(console, 'warn');
|
||||
|
||||
var Anonymous = React.createClass({
|
||||
displayName: undefined,
|
||||
render: function() {
|
||||
return <div />;
|
||||
}
|
||||
});
|
||||
|
||||
var divs = [
|
||||
<div />,
|
||||
<div />
|
||||
];
|
||||
ReactTestUtils.renderIntoDocument(<Anonymous>{divs}</Anonymous>);
|
||||
|
||||
expect(console.warn.argsForCall.length).toBe(1);
|
||||
expect(console.warn.argsForCall[0][0]).toBe(
|
||||
'Warning: Each child in an array or iterator should have a unique ' +
|
||||
'"key" prop. See http://fb.me/react-warning-keys for more information.'
|
||||
);
|
||||
});
|
||||
|
||||
it('warns for keys for arrays of elements with no owner info', function() {
|
||||
spyOn(console, 'warn');
|
||||
|
||||
var divs = [
|
||||
<div />,
|
||||
<div />
|
||||
];
|
||||
ReactTestUtils.renderIntoDocument(<div>{divs}</div>);
|
||||
|
||||
expect(console.warn.argsForCall.length).toBe(1);
|
||||
expect(console.warn.argsForCall[0][0]).toBe(
|
||||
'Warning: Each child in an array or iterator should have a unique ' +
|
||||
'"key" prop. Check the React.render call using <div>. See ' +
|
||||
'http://fb.me/react-warning-keys for more information.'
|
||||
);
|
||||
});
|
||||
|
||||
it('warns for keys for iterables of elements in rest args', function() {
|
||||
spyOn(console, 'warn');
|
||||
var Component = React.createFactory(ComponentClass);
|
||||
|
||||
Reference in New Issue
Block a user