From 43930455de2ce9d6a9d8eb20cbb3ac4be828908f Mon Sep 17 00:00:00 2001 From: CommitSyncScript Date: Wed, 26 Jun 2013 11:20:26 -0700 Subject: [PATCH] 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. --- src/core/ReactCompositeComponent.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index df9e5bbd78..6c4fed8fd7 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -105,9 +105,8 @@ var ReactCompositeComponentInterface = { // ==== Definition methods ==== /** - * Invoked when the component is mounted and whenever new props are received. - * Values in the returned mapping will be set on `this.props` if that prop is - * not specified (i.e. using an `in` check). + * Invoked when the component is mounted. Values in the mapping will be set on + * `this.props` if that prop is not specified (i.e. using an `in` check). * * This method is invoked before `getInitialState` and therefore cannot rely * on `this.state` or use `this.setState`. @@ -504,6 +503,8 @@ var ReactCompositeComponentMixin = { mountComponent: function(rootID, transaction) { ReactComponent.Mixin.mountComponent.call(this, rootID, transaction); this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING; + + this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null; this._processProps(this.props); if (this.__reactAutoBindMap) { @@ -549,6 +550,8 @@ var ReactCompositeComponentMixin = { } this._compositeLifeCycleState = null; + this._defaultProps = null; + ReactComponent.Mixin.unmountComponent.call(this); this._renderedComponent.unmountComponent(); this._renderedComponent = null; @@ -651,12 +654,10 @@ var ReactCompositeComponentMixin = { */ _processProps: function(props) { var propName; - if (this.getDefaultProps) { - var defaultProps = this.getDefaultProps(); - for (propName in defaultProps) { - if (!(propName in props)) { - props[propName] = defaultProps[propName]; - } + var defaultProps = this._defaultProps; + for (propName in defaultProps) { + if (!(propName in props)) { + props[propName] = defaultProps[propName]; } } var propDeclarations = this.constructor.propDeclarations;