From f3aac85d01d87bca593170f6ae0935c9aaf9084c Mon Sep 17 00:00:00 2001 From: ngavalas Date: Fri, 14 Jun 2013 16:37:20 -0700 Subject: [PATCH] Updated docs and check for truthiness Change api docs to reflect presence of the new argument. In addition, callback was change to require only a "truthy" value. --- docs/docs/api.md | 6 ++++-- src/core/ReactCompositeComponent.js | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/docs/api.md b/docs/docs/api.md index fd29070ecb..cc73f2e3a3 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -108,15 +108,17 @@ Transfer properties from this component to a target component that have not alre #### setState ```javascript -setState(object nextState) +setState(object nextState_[, function callback]_) ``` -Merges nextState with the current state. This is the primary method you use to trigger UI updates from event handlers and server request callbacks. +Merges nextState with the current state. This is the primary method you use to trigger UI updates from event handlers and server request callbacks. In addition, you can supply an optional callback function that is executed once `setState` is completed. **Note:** *NEVER* mutate `this.state` directly. As calling `setState()` afterwards may replace the mutation you made. Treat `this.state` as if it were immutable. **Note:** `setState()` does not immediately mutate `this.state` but creates a pending state transition. Accessing `this.state` after calling this method can potentially return the existing value. +**Note**: There is no guarantee of synchronous operation of calls to `setState` and calls may eventually be batched for performance gains. + #### replaceState ```javascript diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index 03f17019f1..7a98769c0d 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -538,8 +538,8 @@ 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 callable, do it. - typeof callback === 'function' && callback(); + // If `callback` is truthy, do it. + callback && callback(); }, /**