mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Don't blow up in key warning for undefined type
This commit is contained in:
1 parent
249e0f7331
commit
1959aaa1f9
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).'
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user