-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Adding optional callback argument to setState
#93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -525,13 +525,19 @@ var ReactCompositeComponentMixin = { | |
| * There is no guarantee that `this.state` will be immediately updated, so | ||
| * accessing `this.state` after calling this method may return the old value. | ||
| * | ||
| * There is no guarantee that calls to `setState` will run synchronously, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. De-dupe this with the preceding comment paragraph? Could just delete the first sentence and smash them together. |
||
| * as they may eventually be batched together. You can provide an optional | ||
| * callback that will be executed when the call to setState is actually | ||
| * completed. | ||
| * | ||
| * @param {object} partialState Next partial state to be merged with state. | ||
| * @param {?function} callback Called after state is updated. | ||
| * @final | ||
| * @protected | ||
| */ | ||
| setState: function(partialState) { | ||
| setState: function(partialState, callback) { | ||
| // Merge with `_pendingState` if it exists, otherwise with existing state. | ||
| this.replaceState(merge(this._pendingState || this.state, partialState)); | ||
| this.replaceState(merge(this._pendingState || this.state, partialState), callback); | ||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -542,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() || | ||
|
|
@@ -582,6 +589,9 @@ var ReactCompositeComponentMixin = { | |
|
|
||
| this._compositeLifeCycleState = null; | ||
| } | ||
|
|
||
| // If callback is 'truthy', execute it | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete unnecessary comment. |
||
| callback && callback(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When this gets executed, there is no guarantee that the state has been executed. In particular, |
||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -704,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(), | ||
|
|
@@ -728,6 +739,8 @@ var ReactCompositeComponentMixin = { | |
| transaction | ||
| ); | ||
| ReactComponent.ReactReconcileTransaction.release(transaction); | ||
|
|
||
| callback && callback(); | ||
| }, | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we put these in ReactCompositeComponent._performComponentUpdate() instead? That way we won't have to copy-paste it everywhere, and it'll be closer to where the batching logic will eventually live.