Skip to content

Commit

Permalink
Addressed Dan's feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Sep 4, 2016
1 parent 6390b8d commit 056666d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Invalid type for createElement
title: Invalid Element Type Warning
layout: single
permalink: warnings/create-element-types.html
permalink: warnings/invalid-element-type.html
---

You probably came here because your code is trying to create a ReactElement with an invalid type using JSX or the `React.createElement` API. This usually happens when you have an invalid import statement.
Expand Down
51 changes: 18 additions & 33 deletions src/isomorphic/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,51 +181,36 @@ function validatePropTypes(element) {
}

function warnInvalidType(inputValue) {
// warning(...) messages need to be string literals.
// If not, we can do a ternary expression to evaluate the need
// for the mistyped import message, and not repeat the message
// twice here.
if (inputValue === undefined) {
warning(
false,
'React.createElement: %s is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or '+
'React.Component (for composite components). ' +
'Did you mistype an import or forget to export your component?' +
'%s See fb.me/react-warning-create-element for more information.',
inputValue, getDeclarationErrorAddendum()
'React.createElement: undefined is an invalid element type. ' +
'Did you mistype an import or forget to export your component? ' +
'It should be a string (for DOM elements), component ' +
'class or function (for user-defined components).' +
'%s See https://fb.me/react-invalid-element-type for more information.',
getDeclarationErrorAddendum()
);
} else {
} else {
warning(
false,
'React.createElement: %s is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or ' +
'React.Component (for composite components).' +
'%s See fb.me/react-warning-create-element for more information.',
inputValue, getDeclarationErrorAddendum()
'React.createElement: %s is an invalid element type. ' +
'It should be a string (for DOM elements), component ' +
'class or function (for user-defined components).' +
'%s See https://fb.me/react-invalid-element-type for more information.',
inputValue,
getDeclarationErrorAddendum()
);
}
};
}

var ReactElementValidator = {

createElement: function(type, props, children) {
var validType = true;

switch (typeof type) {
case 'string':
case 'function':
break;
case 'object':
if (type !== null) {
break;
}
// fallthrough if type is a null
default:
// We warn in this case but don't throw. We expect the element creation to
// succeed and there will likely be errors in render.
validType = false;
warnInvalidType(type);
var validType = typeof type === 'string' || typeof type === 'function' ||
(type !== null && typeof type === 'object');
if (!validType) {
warnInvalidType(type);
}

var element = ReactElement.createElement.apply(this, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,29 +297,29 @@ describe('ReactElementValidator', function() {
React.createElement(123);
expect(console.error.calls.count()).toBe(4);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: React.createElement: undefined is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). ' +
'Warning: React.createElement: undefined is an invalid element type. ' +
'Did you mistype an import or forget to export your component? ' +
'See fb.me/react-warning-create-element for more information.'
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). ' +
'See https://fb.me/react-invalid-element-type for more information.'
);
expect(console.error.calls.argsFor(1)[0]).toBe(
'Warning: React.createElement: null is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). ' +
'See fb.me/react-warning-create-element for more information.'
'Warning: React.createElement: null is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). ' +
'See https://fb.me/react-invalid-element-type for more information.'
);
expect(console.error.calls.argsFor(2)[0]).toBe(
'Warning: React.createElement: true is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). ' +
'See fb.me/react-warning-create-element for more information.'
'Warning: React.createElement: true is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). ' +
'See https://fb.me/react-invalid-element-type for more information.'
);
expect(console.error.calls.argsFor(3)[0]).toBe(
'Warning: React.createElement: 123 is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). ' +
'See fb.me/react-warning-create-element for more information.'
'Warning: React.createElement: 123 is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). ' +
'See https://fb.me/react-invalid-element-type for more information.'
);
React.createElement('div');
expect(console.error.calls.count()).toBe(4);
Expand All @@ -341,10 +341,10 @@ describe('ReactElementValidator', function() {
);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: React.createElement: null is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). Check the render method of `ParentComp`. ' +
'See fb.me/react-warning-create-element for more information.'
'Warning: React.createElement: null is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). Check the render method of `ParentComp`. ' +
'See https://fb.me/react-invalid-element-type for more information.'
);
});

Expand Down Expand Up @@ -542,11 +542,11 @@ describe('ReactElementValidator', function() {
void <Foo>{[<div />]}</Foo>;
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: React.createElement: undefined is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). ' +
'Warning: React.createElement: undefined is an invalid element type. ' +
'Did you mistype an import or forget to export your component? ' +
'See fb.me/react-warning-create-element for more information.'
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). ' +
'See https://fb.me/react-invalid-element-type for more information.'
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,25 @@ describe('ReactJSXElementValidator', function() {
void <Num />;
expect(console.error.calls.count()).toBe(4);
expect(console.error.calls.argsFor(0)[0]).toContain(
'undefined is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components). ' +
'Did you mistype an import or forget to export your component? '
'undefined is an invalid element type. ' +
'Did you mistype an import or forget to export your component? ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). '
);
expect(console.error.calls.argsFor(1)[0]).toContain(
'null is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components).'
'null is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). '
);
expect(console.error.calls.argsFor(2)[0]).toContain(
'true is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components).'
'true is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). '
);
expect(console.error.calls.argsFor(3)[0]).toContain(
'123 is an invalid value for a type. ' +
'It should be a string (for DOM elements), ReactClass or React.Component ' +
'(for composite components).'
'123 is an invalid element type. ' +
'It should be a string (for DOM elements), component class or function ' +
'(for user-defined components). '
);
void <Div />;
expect(console.error.calls.count()).toBe(4);
Expand Down

0 comments on commit 056666d

Please sign in to comment.