Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect better against createElement(null/undefined) #2726

Merged
merged 1 commit into from
Dec 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ ReactElement.createElement = function(type, config, children) {
}

// Resolve default props
if (type.defaultProps) {
if (type && type.defaultProps) {
var defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (typeof props[propName] === 'undefined') {
Expand Down
44 changes: 28 additions & 16 deletions src/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');

var getIteratorFn = require('getIteratorFn');
var monitorCodeUse = require('monitorCodeUse');
var warning = require('warning');

/**
* Warn if there's no key explicitly set on dynamic arrays of children or
Expand Down Expand Up @@ -239,6 +240,15 @@ function checkPropTypes(componentName, propTypes, props, location) {
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).'
);

var element = ReactElement.createElement.apply(this, arguments);

// The result can be nullish if a mock or a custom function is used.
Expand All @@ -251,22 +261,24 @@ var ReactElementValidator = {
validateChildKeys(arguments[i], type);
}

var name = type.displayName;
if (type.propTypes) {
checkPropTypes(
name,
type.propTypes,
element.props,
ReactPropTypeLocations.prop
);
}
if (type.contextTypes) {
checkPropTypes(
name,
type.contextTypes,
element._context,
ReactPropTypeLocations.context
);
if (type) {
var name = type.displayName;
if (type.propTypes) {
checkPropTypes(
name,
type.propTypes,
element.props,
ReactPropTypeLocations.prop
);
}
if (type.contextTypes) {
checkPropTypes(
name,
type.contextTypes,
element._context,
ReactPropTypeLocations.context
);
}
}
return element;
},
Expand Down
22 changes: 22 additions & 0 deletions src/classic/element/__tests__/ReactElementValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ describe('ReactElementValidator', function() {
);
});

it('gives a helpful error when passing null or undefined', function() {
spyOn(console, 'warn');
React.createElement(undefined);
React.createElement(null);
expect(console.warn.calls.length).toBe(2);
expect(console.warn.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).'
);
expect(console.warn.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).'
);
React.createElement('div');
expect(console.warn.calls.length).toBe(2);

expect(() => React.createElement(undefined)).not.toThrow()
});


it('should check default prop values', function() {
spyOn(console, 'warn');

Expand Down