@samitouri / QOS-React-2 / commits / 7a0f2d71bb
Fork 0

The library for web and native user interfaces.

Add callbacks to all public-facing state/props methods

All public facing {set,replace,force}{props,state} methods now support callbacks.

ngavalas committed Jun 16, 2013 at 22:45 UTC 7a0f2d71bbfd6ea03eb239830855d992c1ecb841
2 files changed +17 -8
src/core/ReactComponent.js
+7 -3
@@ -231,21 +231,23 @@ var ReactComponent = {
231 * Sets a subset of the props.
232 *
233 * @param {object} partialProps Subset of the next props.
234 + * @param {?function} callback Called after props are updated.
235 * @final
236 * @public
237 */
237 - setProps: function(partialProps) {
238 - this.replaceProps(merge(this.props, partialProps));
238 + setProps: function(partialProps, callback) {
239 + this.replaceProps(merge(this.props, partialProps), callback);
240 },
241
242 /**
243 * Replaces all of the props.
244 *
245 * @param {object} props New props.
246 + * @param {?function} callback Called after props are updated.
247 * @final
248 * @public
249 */
248 - replaceProps: function(props) {
250 + replaceProps: function(props, callback) {
251 invariant(
252 !this.props[OWNER],
253 'replaceProps(...): You called `setProps` or `replaceProps` on a ' +
@@ -257,6 +259,8 @@ var ReactComponent = {
259 var transaction = ReactComponent.ReactReconcileTransaction.getPooled();
260 transaction.perform(this.receiveProps, this, props, transaction);
261 ReactComponent.ReactReconcileTransaction.release(transaction);
262 +
263 + callback && callback();
264 },
265
266 /**
src/core/ReactCompositeComponent.js
+10 -5
@@ -537,9 +537,7 @@ var ReactCompositeComponentMixin = {
537 */
538 setState: function(partialState, callback) {
539 // Merge with `_pendingState` if it exists, otherwise with existing state.
540 - this.replaceState(merge(this._pendingState || this.state, partialState));
541 - // If `callback` is truthy, do it.
542 - callback && callback();
540 + this.replaceState(merge(this._pendingState || this.state, partialState), callback);
541 },
542
543 /**
@@ -550,10 +548,11 @@ var ReactCompositeComponentMixin = {
548 * accessing `this.state` after calling this method may return the old value.
549 *
550 * @param {object} completeState Next state.
551 + * @param {?function} callback Called after state is updated.
552 * @final
553 * @protected
554 */
556 - replaceState: function(completeState) {
555 + replaceState: function(completeState, callback) {
556 var compositeLifeCycleState = this._compositeLifeCycleState;
557 invariant(
558 this.isMounted() ||
@@ -590,6 +589,9 @@ var ReactCompositeComponentMixin = {
589
590 this._compositeLifeCycleState = null;
591 }
592 +
593 + // If callback is 'truthy', execute it
594 + callback && callback();
595 },
596
597 /**
@@ -712,10 +714,11 @@ var ReactCompositeComponentMixin = {
714 * This will not invoke `shouldUpdateComponent`, but it will invoke
715 * `componentWillUpdate` and `componentDidUpdate`.
716 *
717 + * @param {?function} callback Called after update is complete.
718 * @final
719 * @protected
720 */
718 - forceUpdate: function() {
721 + forceUpdate: function(callback) {
722 var compositeLifeCycleState = this._compositeLifeCycleState;
723 invariant(
724 this.isMounted(),
@@ -736,6 +739,8 @@ var ReactCompositeComponentMixin = {
739 transaction
740 );
741 ReactComponent.ReactReconcileTransaction.release(transaction);
742 +
743 + callback && callback();
744 },
745
746 /**