Merge pull request #5041 from spicyj/val-key-err

Don't blow up in key warning for undefined type
This commit is contained in:
Ben Alpert committed 2015-10-05 13:57:25 -07:00
commit 94ccda8543
2 files changed
+24 -3

No files matched your search

@@ -254,9 +254,11 @@ function validatePropTypes(element) {
var ReactElementValidator = {
createElement: function(type, props, children) {
var validType = typeof type === 'string' || typeof type === 'function';
// We warn in this case but don't throw. We expect the element creation to
// succeed and there will likely be errors in render.
warning(typeof type === 'string' || typeof type === 'function',
warning(
validType,
'React.createElement: type should not be null, undefined, boolean, or ' +
'number. It should be a string (for DOM elements) or a ReactClass ' +
'(for composite components).%s',
@@ -271,8 +273,15 @@ var ReactElementValidator = {
return element;
}
for (var i = 2; i < arguments.length; i++) {
validateChildKeys(arguments[i], type);
// Skip key warning if the type isn't valid since our key validation logic
// doesn't expect a non-string/function type and can throw confusing errors.
// We don't want exception behavior to differ between dev and prod.
// (Rendering will throw with a helpful message and as soon as the type is
// fixed, the key warnings will appear.)
if (validType) {
for (var i = 2; i < arguments.length; i++) {
validateChildKeys(arguments[i], type);
}
}
validatePropTypes(element);
@@ -483,4 +483,16 @@ describe('ReactElementValidator', function() {
void <div>{[child]}</div>;
});
it('does not blow up on key warning with undefined type', function() {
spyOn(console, 'error');
var Foo = undefined;
void <Foo>{[<div />]}</Foo>;
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: React.createElement: type should not be null, undefined, ' +
'boolean, or number. It should be a string (for DOM elements) or a ' +
'ReactClass (for composite components).'
);
});
});