Skip to content

Commit

Permalink
Move defaultProps resolution to the descriptor factory
Browse files Browse the repository at this point in the history
Moves the defaultProps resolution to the descriptor factory.
  • Loading branch information
sebmarkbage authored and zpao committed Jul 19, 2014
1 parent 3a7dbe6 commit c419cce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
16 changes: 2 additions & 14 deletions src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,25 +939,13 @@ var ReactCompositeComponentMixin = {
* @private
*/
_processProps: function(newProps) {
var defaultProps = this.constructor.defaultProps;
var props;
if (defaultProps) {
props = merge(newProps);
for (var propName in defaultProps) {
if (typeof props[propName] === 'undefined') {
props[propName] = defaultProps[propName];
}
}
} else {
props = newProps;
}
if (__DEV__) {
var propTypes = this.constructor.propTypes;
if (propTypes) {
this._checkPropTypes(propTypes, props, ReactPropTypeLocations.prop);
this._checkPropTypes(propTypes, newProps, ReactPropTypeLocations.prop);
}
}
return props;
return newProps;
},

/**
Expand Down
16 changes: 13 additions & 3 deletions src/core/ReactDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
}
}

var propName;

// Reserved names are extracted
var props = {};

Expand All @@ -168,16 +170,14 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
ref = config.ref === undefined ? null : config.ref;
key = config.key === undefined ? null : '' + config.key;
// Remaining properties are added to a new props object
for (var propName in config) {
for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
props[propName] = config[propName];
}
}
}

// TODO: fill in defaultProps here, after transferPropsTo is gone

// Children can be more than one argument, and those are transferred onto
// the newly allocated props object.
var childrenLength = arguments.length - 2;
Expand All @@ -191,6 +191,16 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
props.children = childArray;
}

// Resolve default props
if (type.defaultProps) {
var defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (typeof props[propName] === 'undefined') {
props[propName] = defaultProps[propName];
}
}
}

return new ReactDescriptor(
type,
key,
Expand Down
4 changes: 2 additions & 2 deletions src/core/__tests__/ReactPropTransferer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('ReactPropTransferer', function() {
);
});

it('should not use the default when a prop is transfered', function() {
it('uses the default instead of the transferred prop (regress)', function() {

var Child = React.createClass({

Expand All @@ -175,7 +175,7 @@ describe('ReactPropTransferer', function() {
},

render: function() {
expect(this.props.x).toBe(5);
expect(this.props.x).toBe(2);
return <div />;
}

Expand Down

0 comments on commit c419cce

Please sign in to comment.