mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
@@ -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();
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user