Skip to content
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 use undefined as parent name in key warning #3275

Merged
merged 1 commit into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] || (
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/classic/element/__tests__/ReactElementValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down