mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
Merge pull request #3521 from grant/master
Display error when trying to create an element of type `boolean`. Fixes #3478
This commit is contained in:
@@ -362,7 +362,8 @@ function checkAndWarnForMutatedProps(element) {
|
||||
* @param {ReactElement} element
|
||||
*/
|
||||
function validatePropTypes(element) {
|
||||
if (element.type == null) {
|
||||
if (!(typeof element.type === 'string' ||
|
||||
typeof element.type === 'function')) {
|
||||
// This has already warned. Don't throw.
|
||||
return;
|
||||
}
|
||||
@@ -398,11 +399,10 @@ var ReactElementValidator = {
|
||||
createElement: function(type, props, children) {
|
||||
// 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(
|
||||
type != null,
|
||||
'React.createElement: type should not be null or undefined. It should ' +
|
||||
'be a string (for DOM elements) or a ReactClass (for composite ' +
|
||||
'components).%s',
|
||||
warning(typeof type === 'string' || typeof type === 'function',
|
||||
'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',
|
||||
getDeclarationErrorAddendum()
|
||||
);
|
||||
|
||||
|
||||
@@ -258,26 +258,40 @@ describe('ReactElementValidator', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('gives a helpful error when passing null or undefined', function() {
|
||||
it('gives a helpful error when passing null, undefined, boolean, or number',
|
||||
() => {
|
||||
spyOn(console, 'error');
|
||||
React.createElement(undefined);
|
||||
React.createElement(null);
|
||||
expect(console.error.calls.length).toBe(2);
|
||||
React.createElement(true);
|
||||
React.createElement(123);
|
||||
expect(console.error.calls.length).toBe(4);
|
||||
expect(console.error.calls[0].args[0]).toBe(
|
||||
'Warning: React.createElement: type should not be null or undefined. ' +
|
||||
'It should be a string (for DOM elements) or a ReactClass (for ' +
|
||||
'composite components).'
|
||||
'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).'
|
||||
);
|
||||
expect(console.error.calls[1].args[0]).toBe(
|
||||
'Warning: React.createElement: type should not be null or undefined. ' +
|
||||
'It should be a string (for DOM elements) or a ReactClass (for ' +
|
||||
'composite components).'
|
||||
'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).'
|
||||
);
|
||||
expect(console.error.calls[2].args[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).'
|
||||
);
|
||||
expect(console.error.calls[3].args[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).'
|
||||
);
|
||||
React.createElement('div');
|
||||
expect(console.error.calls.length).toBe(2);
|
||||
expect(console.error.calls.length).toBe(4);
|
||||
});
|
||||
|
||||
it('includes the owner name when passing null or undefined', function() {
|
||||
it('includes the owner name when passing null, undefined, boolean, or number',
|
||||
() => {
|
||||
spyOn(console, 'error');
|
||||
var ParentComp = React.createClass({
|
||||
render: function() {
|
||||
@@ -289,9 +303,10 @@ describe('ReactElementValidator', function() {
|
||||
}).toThrow();
|
||||
expect(console.error.calls.length).toBe(2);
|
||||
expect(console.error.calls[0].args[0]).toBe(
|
||||
'Warning: React.createElement: type should not be null or undefined. ' +
|
||||
'It should be a string (for DOM elements) or a ReactClass (for ' +
|
||||
'composite components). Check the render method of `ParentComp`.'
|
||||
'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). Check the render method of ' +
|
||||
'`ParentComp`.'
|
||||
);
|
||||
expect(console.error.calls[1].args[0]).toBe(
|
||||
'Warning: Only functions or strings can be mounted as React components.'
|
||||
|
||||
@@ -208,24 +208,36 @@ describe('ReactJSXElementValidator', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('gives a helpful error when passing null or undefined', function() {
|
||||
it('gives a helpful error when passing null, undefined, or boolean', () => {
|
||||
var Undefined = undefined;
|
||||
var Null = null;
|
||||
var True = true;
|
||||
var Num = 123;
|
||||
var Div = 'div';
|
||||
spyOn(console, 'error');
|
||||
<Undefined />;
|
||||
<Null />;
|
||||
expect(console.error.calls.length).toBe(2);
|
||||
<True />;
|
||||
<Num />;
|
||||
expect(console.error.calls.length).toBe(4);
|
||||
expect(console.error.calls[0].args[0]).toContain(
|
||||
'type should not be null or undefined. It should be a string (for ' +
|
||||
'DOM elements) or a ReactClass (for composite components).'
|
||||
'type should not be null, undefined, boolean, or number. It should be ' +
|
||||
'a string (for DOM elements) or a ReactClass (for composite components).'
|
||||
);
|
||||
expect(console.error.calls[1].args[0]).toContain(
|
||||
'type should not be null or undefined. It should be a string (for ' +
|
||||
'DOM elements) or a ReactClass (for composite components).'
|
||||
'type should not be null, undefined, boolean, or number. It should be ' +
|
||||
'a string (for DOM elements) or a ReactClass (for composite components).'
|
||||
);
|
||||
expect(console.error.calls[2].args[0]).toContain(
|
||||
'type should not be null, undefined, boolean, or number. It should be ' +
|
||||
'a string (for DOM elements) or a ReactClass (for composite components).'
|
||||
);
|
||||
expect(console.error.calls[3].args[0]).toContain(
|
||||
'type should not be null, undefined, boolean, or number. It should be ' +
|
||||
'a string (for DOM elements) or a ReactClass (for composite components).'
|
||||
);
|
||||
<Div />;
|
||||
expect(console.error.calls.length).toBe(2);
|
||||
expect(console.error.calls.length).toBe(4);
|
||||
});
|
||||
|
||||
it('should check default prop values', function() {
|
||||
|
||||
Reference in New Issue
Block a user