From c419cce5c9eeaf4d41f226016c852500bdcb4f71 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Fri, 18 Jul 2014 10:04:20 -0700 Subject: [PATCH] Move defaultProps resolution to the descriptor factory Moves the defaultProps resolution to the descriptor factory. --- src/core/ReactCompositeComponent.js | 16 ++-------------- src/core/ReactDescriptor.js | 16 +++++++++++++--- src/core/__tests__/ReactPropTransferer-test.js | 4 ++-- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index 34739252ef..825c23504d 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -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; }, /** diff --git a/src/core/ReactDescriptor.js b/src/core/ReactDescriptor.js index 6bd4878421..e89daff9fd 100644 --- a/src/core/ReactDescriptor.js +++ b/src/core/ReactDescriptor.js @@ -158,6 +158,8 @@ ReactDescriptor.createDescriptor = function(type, config, children) { } } + var propName; + // Reserved names are extracted var props = {}; @@ -168,7 +170,7 @@ 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]; @@ -176,8 +178,6 @@ ReactDescriptor.createDescriptor = function(type, config, children) { } } - // 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; @@ -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, diff --git a/src/core/__tests__/ReactPropTransferer-test.js b/src/core/__tests__/ReactPropTransferer-test.js index ec2616ace4..9373fa0ad2 100644 --- a/src/core/__tests__/ReactPropTransferer-test.js +++ b/src/core/__tests__/ReactPropTransferer-test.js @@ -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({ @@ -175,7 +175,7 @@ describe('ReactPropTransferer', function() { }, render: function() { - expect(this.props.x).toBe(5); + expect(this.props.x).toBe(2); return
; }