@samitouri / QOS-React-2 / commits / c419cce5c9

Move defaultProps resolution to the descriptor factory

Moves the defaultProps resolution to the descriptor factory.

Sebastian Markbage committed Jul 18, 2014 at 10:04 UTC c419cce5c9eeaf4d41f226016c852500bdcb4f71
3 files changed +17 -19
src/core/ReactCompositeComponent.js
+2 -14
@@ -939,25 +939,13 @@ var ReactCompositeComponentMixin = {
939 * @private
940 */
941 _processProps: function(newProps) {
942 - var defaultProps = this.constructor.defaultProps;
943 - var props;
944 - if (defaultProps) {
945 - props = merge(newProps);
946 - for (var propName in defaultProps) {
947 - if (typeof props[propName] === 'undefined') {
948 - props[propName] = defaultProps[propName];
949 - }
950 - }
951 - } else {
952 - props = newProps;
953 - }
942 if (__DEV__) {
943 var propTypes = this.constructor.propTypes;
944 if (propTypes) {
957 - this._checkPropTypes(propTypes, props, ReactPropTypeLocations.prop);
945 + this._checkPropTypes(propTypes, newProps, ReactPropTypeLocations.prop);
946 }
947 }
960 - return props;
948 + return newProps;
949 },
950
951 /**
src/core/ReactDescriptor.js
+13 -3
@@ -158,6 +158,8 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
158 }
159 }
160
161 + var propName;
162 +
163 // Reserved names are extracted
164 var props = {};
165
@@ -168,7 +170,7 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
170 ref = config.ref === undefined ? null : config.ref;
171 key = config.key === undefined ? null : '' + config.key;
172 // Remaining properties are added to a new props object
171 - for (var propName in config) {
173 + for (propName in config) {
174 if (config.hasOwnProperty(propName) &&
175 !RESERVED_PROPS.hasOwnProperty(propName)) {
176 props[propName] = config[propName];
@@ -176,8 +178,6 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
178 }
179 }
180
179 - // TODO: fill in defaultProps here, after transferPropsTo is gone
180 -
181 // Children can be more than one argument, and those are transferred onto
182 // the newly allocated props object.
183 var childrenLength = arguments.length - 2;
@@ -191,6 +191,16 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
191 props.children = childArray;
192 }
193
194 + // Resolve default props
195 + if (type.defaultProps) {
196 + var defaultProps = type.defaultProps;
197 + for (propName in defaultProps) {
198 + if (typeof props[propName] === 'undefined') {
199 + props[propName] = defaultProps[propName];
200 + }
201 + }
202 + }
203 +
204 return new ReactDescriptor(
205 type,
206 key,
src/core/__tests__/ReactPropTransferer-test.js
+2 -2
@@ -164,7 +164,7 @@ describe('ReactPropTransferer', function() {
164 );
165 });
166
167 - it('should not use the default when a prop is transfered', function() {
167 + it('uses the default instead of the transferred prop (regress)', function() {
168
169 var Child = React.createClass({
170
@@ -175,7 +175,7 @@ describe('ReactPropTransferer', function() {
175 },
176
177 render: function() {
178 - expect(this.props.x).toBe(5);
178 + expect(this.props.x).toBe(2);
179 return <div />;
180 }
181