From d5fa14de1a7babf4e80288f6914e94d160667af9 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Thu, 26 Feb 2015 15:20:37 -0800 Subject: [PATCH] Don't use undefined as parent name in key warning Fixes #3222. --- src/classic/element/ReactElementValidator.js | 10 +++-- .../__tests__/ReactElementValidator-test.js | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/classic/element/ReactElementValidator.js b/src/classic/element/ReactElementValidator.js index 35cd38d6cf..2cf89f98e2 100644 --- a/src/classic/element/ReactElementValidator.js +++ b/src/classic/element/ReactElementValidator.js @@ -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 diff --git a/src/classic/element/__tests__/ReactElementValidator-test.js b/src/classic/element/__tests__/ReactElementValidator-test.js index 9555335c48..98de236f28 100644 --- a/src/classic/element/__tests__/ReactElementValidator-test.js +++ b/src/classic/element/__tests__/ReactElementValidator-test.js @@ -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
; + } + }); + + var divs = [ +
, +
+ ]; + ReactTestUtils.renderIntoDocument({divs}); + + 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 = [ +
, +
+ ]; + ReactTestUtils.renderIntoDocument(
{divs}
); + + 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
. 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);