The library for web and native user interfaces.
Cache Default Props
The `getDefaultProps` return value should not be dependent on any external data (including `this.props` and `this.state`), so the return value should be consistent everytime we call it. This caches the return value so we do not do work and allocate memory unnecessarily.
CommitSyncScript committed
Jun 26, 2013 at 11:20 UTC
43930455de2ce9d6a9d8eb20cbb3ac4be828908f
1 file changed
+10
-9
src/core/ReactCompositeComponent.js
+10
-9
@@ -105,9 +105,8 @@ var ReactCompositeComponentInterface = {
105
// ==== Definition methods ====
106
107
/**
108
- * Invoked when the component is mounted and whenever new props are received.
109
- * Values in the returned mapping will be set on `this.props` if that prop is
110
- * not specified (i.e. using an `in` check).
108
+ * Invoked when the component is mounted. Values in the mapping will be set on
109
+ * `this.props` if that prop is not specified (i.e. using an `in` check).
110
*
111
* This method is invoked before `getInitialState` and therefore cannot rely
112
* on `this.state` or use `this.setState`.
@@ -504,6 +503,8 @@ var ReactCompositeComponentMixin = {
503
mountComponent: function(rootID, transaction) {
504
ReactComponent.Mixin.mountComponent.call(this, rootID, transaction);
505
this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING;
506
+
507
+ this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null;
508
this._processProps(this.props);
509
510
if (this.__reactAutoBindMap) {
@@ -549,6 +550,8 @@ var ReactCompositeComponentMixin = {
550
}
551
this._compositeLifeCycleState = null;
552
553
+ this._defaultProps = null;
554
+
555
ReactComponent.Mixin.unmountComponent.call(this);
556
this._renderedComponent.unmountComponent();
557
this._renderedComponent = null;
@@ -651,12 +654,10 @@ var ReactCompositeComponentMixin = {
654
*/
655
_processProps: function(props) {
656
var propName;
654
- if (this.getDefaultProps) {
655
- var defaultProps = this.getDefaultProps();
656
- for (propName in defaultProps) {
657
- if (!(propName in props)) {
658
- props[propName] = defaultProps[propName];
659
- }
657
+ var defaultProps = this._defaultProps;
658
+ for (propName in defaultProps) {
659
+ if (!(propName in props)) {
660
+ props[propName] = defaultProps[propName];
661
}
662
}
663
var propDeclarations = this.constructor.propDeclarations;