Add callbacks to all public-facing state/props methods

All public facing {set,replace,force}{props,state} methods now support
callbacks.
This commit is contained in:
ngavalas
2013-06-16 22:45:36 -07:00
parent c81cc2e6d5
commit 7a0f2d71bb
2 changed files with 17 additions and 8 deletions
+7 -3
View File
@@ -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();
},
/**
+10 -5
View File
@@ -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();
},
/**