Don't blow up in key warning for undefined type
Ben Alpert committed
Oct 2, 2015 at 11:31 UTC
1959aaa1f9941b369a10cb7a93b7e6de84d1d887
2 files changed
+24
-3
src/isomorphic/classic/element/ReactElementValidator.js
+12
-3
@@ -254,9 +254,11 @@ function validatePropTypes(element) {
254
var ReactElementValidator = {
255
256
createElement: function(type, props, children) {
257
+ var validType = typeof type === 'string' || typeof type === 'function';
258
// We warn in this case but don't throw. We expect the element creation to
259
// succeed and there will likely be errors in render.
259
- warning(typeof type === 'string' || typeof type === 'function',
260
+ warning(
261
+ validType,
262
'React.createElement: type should not be null, undefined, boolean, or ' +
263
'number. It should be a string (for DOM elements) or a ReactClass ' +
264
'(for composite components).%s',
@@ -271,8 +273,15 @@ var ReactElementValidator = {
273
return element;
274
}
275
274
- for (var i = 2; i < arguments.length; i++) {
275
- validateChildKeys(arguments[i], type);
276
+ // Skip key warning if the type isn't valid since our key validation logic
277
+ // doesn't expect a non-string/function type and can throw confusing errors.
278
+ // We don't want exception behavior to differ between dev and prod.
279
+ // (Rendering will throw with a helpful message and as soon as the type is
280
+ // fixed, the key warnings will appear.)
281
+ if (validType) {
282
+ for (var i = 2; i < arguments.length; i++) {
283
+ validateChildKeys(arguments[i], type);
284
+ }
285
}
286
287
validatePropTypes(element);
src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js
+12
@@ -483,4 +483,16 @@ describe('ReactElementValidator', function() {
483
void <div>{[child]}</div>;
484
});
485
486
+ it('does not blow up on key warning with undefined type', function() {
487
+ spyOn(console, 'error');
488
+ var Foo = undefined;
489
+ void <Foo>{[<div />]}</Foo>;
490
+ expect(console.error.argsForCall.length).toBe(1);
491
+ expect(console.error.argsForCall[0][0]).toBe(
492
+ 'Warning: React.createElement: type should not be null, undefined, ' +
493
+ 'boolean, or number. It should be a string (for DOM elements) or a ' +
494
+ 'ReactClass (for composite components).'
495
+ );
496
+ });
497
+
498
});