Skip to content

Commit

Permalink
Warn if props obj passed into createElement / cloneElement inherits f…
Browse files Browse the repository at this point in the history
…rom anything other than Object
  • Loading branch information
richardscarrott committed Feb 27, 2016
1 parent 5bb4303 commit 87a6e93
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/isomorphic/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ ReactElement.createElement = function(type, config, children) {

if (config != null) {
if (__DEV__) {
warning(
!(config.__proto__ != null && config.__proto__ !== Object.prototype),
'React.createElement(...): only instance properties will become props'
);
ref = !config.hasOwnProperty('ref') ||
Object.getOwnPropertyDescriptor(config, 'ref').get ? null : config.ref;
key = !config.hasOwnProperty('key') ||
Expand Down Expand Up @@ -269,6 +273,12 @@ ReactElement.cloneElement = function(element, config, children) {
var owner = element._owner;

if (config != null) {
if (__DEV__) {
warning(
!(config.__proto__ != null && config.__proto__ !== Object.prototype),
'React.cloneElement(...): only instance properties will become props'
);
}
if (config.ref !== undefined) {
// Silently steal the ref from the parent.
ref = config.ref;
Expand Down
11 changes: 11 additions & 0 deletions src/isomorphic/classic/element/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ describe('ReactElement', function() {
expect(element.props.foo).toBe(1);
});

it('warns if the config object inherits from any type other than Object', function() {
spyOn(console, 'error');
React.createElement('div', {foo: 1});
expect(console.error).not.toHaveBeenCalled();
React.createElement('div', Object.create({foo: 1}));
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
'React.createElement(...): only instance properties will become props'
);
});

it('extracts key and ref from the config', function() {
var element = React.createFactory(ComponentClass)({
key: '12',
Expand Down
11 changes: 11 additions & 0 deletions src/isomorphic/classic/element/__tests__/ReactElementClone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ describe('ReactElementClone', function() {
expect(ReactDOM.findDOMNode(component).childNodes[0].className).toBe('xyz');
});

it('should warn if the config object inherits from any type other than Object', function() {
spyOn(console, 'error');
React.cloneElement('div', {foo: 1});
expect(console.error).not.toHaveBeenCalled();
React.cloneElement('div', Object.create({foo: 1}));
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
'React.cloneElement(...): only instance properties will become props'
);
});

it('should keep the original ref if it is not overridden', function() {
var Grandparent = React.createClass({
render: function() {
Expand Down

0 comments on commit 87a6e93

Please sign in to comment.