From 7a0f2d71bbfd6ea03eb239830855d992c1ecb841 Mon Sep 17 00:00:00 2001 From: ngavalas Date: Sun, 16 Jun 2013 22:45:36 -0700 Subject: [PATCH] Add callbacks to all public-facing state/props methods All public facing {set,replace,force}{props,state} methods now support callbacks. --- src/core/ReactComponent.js | 10 +++++++--- src/core/ReactCompositeComponent.js | 15 ++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/core/ReactComponent.js b/src/core/ReactComponent.js index b9aa2330c4..3353310b1f 100644 --- a/src/core/ReactComponent.js +++ b/src/core/ReactComponent.js @@ -231,21 +231,23 @@ var ReactComponent = { * Sets a subset of the props. * * @param {object} partialProps Subset of the next props. + * @param {?function} callback Called after props are updated. * @final * @public */ - setProps: function(partialProps) { - this.replaceProps(merge(this.props, partialProps)); + setProps: function(partialProps, callback) { + this.replaceProps(merge(this.props, partialProps), callback); }, /** * Replaces all of the props. * * @param {object} props New props. + * @param {?function} callback Called after props are updated. * @final * @public */ - replaceProps: function(props) { + replaceProps: function(props, callback) { invariant( !this.props[OWNER], 'replaceProps(...): You called `setProps` or `replaceProps` on a ' + @@ -257,6 +259,8 @@ var ReactComponent = { var transaction = ReactComponent.ReactReconcileTransaction.getPooled(); transaction.perform(this.receiveProps, this, props, transaction); ReactComponent.ReactReconcileTransaction.release(transaction); + + callback && callback(); }, /** diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index 7a98769c0d..78041c2a05 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -537,9 +537,7 @@ var ReactCompositeComponentMixin = { */ setState: function(partialState, callback) { // Merge with `_pendingState` if it exists, otherwise with existing state. - this.replaceState(merge(this._pendingState || this.state, partialState)); - // If `callback` is truthy, do it. - callback && callback(); + this.replaceState(merge(this._pendingState || this.state, partialState), callback); }, /** @@ -550,10 +548,11 @@ var ReactCompositeComponentMixin = { * accessing `this.state` after calling this method may return the old value. * * @param {object} completeState Next state. + * @param {?function} callback Called after state is updated. * @final * @protected */ - replaceState: function(completeState) { + replaceState: function(completeState, callback) { var compositeLifeCycleState = this._compositeLifeCycleState; invariant( this.isMounted() || @@ -590,6 +589,9 @@ var ReactCompositeComponentMixin = { this._compositeLifeCycleState = null; } + + // If callback is 'truthy', execute it + callback && callback(); }, /** @@ -712,10 +714,11 @@ var ReactCompositeComponentMixin = { * This will not invoke `shouldUpdateComponent`, but it will invoke * `componentWillUpdate` and `componentDidUpdate`. * + * @param {?function} callback Called after update is complete. * @final * @protected */ - forceUpdate: function() { + forceUpdate: function(callback) { var compositeLifeCycleState = this._compositeLifeCycleState; invariant( this.isMounted(), @@ -736,6 +739,8 @@ var ReactCompositeComponentMixin = { transaction ); ReactComponent.ReactReconcileTransaction.release(transaction); + + callback && callback(); }, /**