Don't use undefined as parent name in key warning
Fixes #3222.
Ben Alpert committed
Feb 26, 2015 at 15:20 UTC
d5fa14de1a7babf4e80288f6914e94d160667af9
2 files changed
+46
-4
src/classic/element/ReactElementValidator.js
+6
-4
@@ -134,7 +134,8 @@ function validatePropertyKey(name, element, parentType) {
134
*/
135
function warnAndMonitorForKeyUse(message, element, parentType) {
136
var ownerName = getCurrentOwnerDisplayName();
137
- var parentName = parentType.displayName || parentType.name;
137
+ var parentName = typeof parentType === 'string' ?
138
+ parentType : parentType.displayName || parentType.name;
139
140
var useName = ownerName || parentName;
141
var memoizer = ownerHasKeyUseWarning[message] || (
@@ -145,9 +146,10 @@ function warnAndMonitorForKeyUse(message, element, parentType) {
146
}
147
memoizer[useName] = true;
148
148
- message += ownerName ?
149
- ` Check the render method of ${ownerName}.` :
150
- ` Check the React.render call using <${parentName}>.`;
149
+ message +=
150
+ ownerName ? ` Check the render method of ${ownerName}.` :
151
+ parentName ? ` Check the React.render call using <${parentName}>.` :
152
+ '';
153
154
// Usually the current owner is the offender, but if it accepts children as a
155
// property, it may be the creator of the child that's responsible for
src/classic/element/__tests__/ReactElementValidator-test.js
+40
@@ -80,6 +80,46 @@ describe('ReactElementValidator', function() {
80
);
81
});
82
83
+ it('warns for keys for arrays with no owner or parent info', function() {
84
+ spyOn(console, 'warn');
85
+
86
+ var Anonymous = React.createClass({
87
+ displayName: undefined,
88
+ render: function() {
89
+ return <div />;
90
+ }
91
+ });
92
+
93
+ var divs = [
94
+ <div />,
95
+ <div />
96
+ ];
97
+ ReactTestUtils.renderIntoDocument(<Anonymous>{divs}</Anonymous>);
98
+
99
+ expect(console.warn.argsForCall.length).toBe(1);
100
+ expect(console.warn.argsForCall[0][0]).toBe(
101
+ 'Warning: Each child in an array or iterator should have a unique ' +
102
+ '"key" prop. See http://fb.me/react-warning-keys for more information.'
103
+ );
104
+ });
105
+
106
+ it('warns for keys for arrays of elements with no owner info', function() {
107
+ spyOn(console, 'warn');
108
+
109
+ var divs = [
110
+ <div />,
111
+ <div />
112
+ ];
113
+ ReactTestUtils.renderIntoDocument(<div>{divs}</div>);
114
+
115
+ expect(console.warn.argsForCall.length).toBe(1);
116
+ expect(console.warn.argsForCall[0][0]).toBe(
117
+ 'Warning: Each child in an array or iterator should have a unique ' +
118
+ '"key" prop. Check the React.render call using <div>. See ' +
119
+ 'http://fb.me/react-warning-keys for more information.'
120
+ );
121
+ });
122
+
123
it('warns for keys for iterables of elements in rest args', function() {
124
spyOn(console, 'warn');
125
var Component = React.createFactory(ComponentClass);