Skip to content

Commit

Permalink
Not override props with undefined in cloneElement
Browse files Browse the repository at this point in the history
  • Loading branch information
truongduy134 committed Feb 8, 2016
1 parent 725a723 commit 190950f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/isomorphic/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ ReactElement.cloneElement = function(element, config, children) {
// Remaining properties override existing props
for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
!RESERVED_PROPS.hasOwnProperty(propName) &&
config[propName] !== undefined) {
props[propName] = config[propName];
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/isomorphic/classic/element/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,25 @@ describe('ReactElement', function() {
expect(inst2.props.prop).toBe(null);
});

it('does not override props for undefined config key in cloning', function() {
var Component = React.createClass({
getDefaultProps: function() {
return {prop: 'testKey'};
},
render: function() {
return React.createElement('span', null, this.props.prop);
},
});
var instance = React.createElement(Component);

var clonedInstance = React.cloneElement(instance, {prop: undefined});
expect(clonedInstance.props.prop).toBe('testKey');

// Still override for null value
var clonedInstance2 = React.cloneElement(instance, {prop: null});
expect(clonedInstance2.props.prop).toBe(null);
});

it('throws when changing a prop (in dev) after element creation', function() {
var Outer = React.createClass({
render: function() {
Expand Down

0 comments on commit 190950f

Please sign in to comment.