The library for web and native user interfaces.
Adds optional callback to `setState`
This commit adds an optional callback as a second argument to `setState`, to be called after `setState` runs. We never guarantee synchronous execution of `setState`, and as per @phunt, we don't want to make that guarantee because we may eventually batch calls to `setState`. @jwalke agrees with him.
ngavalas committed
Jun 14, 2013 at 16:23 UTC
c6665e346030f73b7ebb615ce1affe8396b0dab8
1 file changed
+9
-1
src/core/ReactCompositeComponent.js
+9
-1
@@ -525,13 +525,21 @@ var ReactCompositeComponentMixin = {
525
* There is no guarantee that `this.state` will be immediately updated, so
526
* accessing `this.state` after calling this method may return the old value.
527
*
528
+ * There is no guarantee that calls to `setState` will run synchronously,
529
+ * as they may eventually be batched together. You can provide an optional
530
+ * callback that will be executed when the call to setState is actually
531
+ * completed.
532
+ *
533
* @param {object} partialState Next partial state to be merged with state.
534
+ * @param {?function} callback Called after state is updated.
535
* @final
536
* @protected
537
*/
532
- setState: function(partialState) {
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 callable, do it.
542
+ typeof callback === 'function' && callback();
543
},
544
545
/**