From 1959aaa1f9941b369a10cb7a93b7e6de84d1d887 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Fri, 2 Oct 2015 11:31:28 -0700 Subject: [PATCH] Don't blow up in key warning for undefined type --- .../classic/element/ReactElementValidator.js | 15 ++++++++++++--- .../__tests__/ReactElementValidator-test.js | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/isomorphic/classic/element/ReactElementValidator.js b/src/isomorphic/classic/element/ReactElementValidator.js index 2d2272e8a2..a4ae328fa6 100644 --- a/src/isomorphic/classic/element/ReactElementValidator.js +++ b/src/isomorphic/classic/element/ReactElementValidator.js @@ -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); diff --git a/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js b/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js index 3200014289..089533d971 100644 --- a/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js +++ b/src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js @@ -483,4 +483,16 @@ describe('ReactElementValidator', function() { void
{[child]}
; }); + it('does not blow up on key warning with undefined type', function() { + spyOn(console, 'error'); + var Foo = undefined; + void {[
]}; + 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).' + ); + }); + });