Skip to content

Conversation

@ngavalas
Copy link
Contributor

We eventually want to batch calls to setState for performance reasons, and this no longer guarantees immediate, synchronous execution of these calls to setState. As such, this commit adds an optional callback parameter so that actions can be taken once the call actually runs.

@phunt says we shouldn't make the synchronous operation guarantee and @jordow agrees.

ngavalas added 3 commits June 14, 2013 16:23
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.
Change api docs to reflect presence of the new argument.  In addition,
callback was change to require only a "truthy" value.
Small problem with markdown syntax in syntax-highlighted block.
@jordwalke
Copy link
Contributor

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be flowed through as a 2nd arg to this.replaceState, another function accessible by client code and where the ultimate work of generalizing this will go.

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

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.

@petehunt
Copy link
Contributor

A simple test case here would be great too, since it's unlikely this will have many callsites in prod code for a while it may break and go unnoticed without one.

@ngavalas
Copy link
Contributor Author

Will add unit tests.

About putting it _performComponentUpdate(), that's where I originally was trying to put it, but I see a small problem with replaceState in particular. Perhaps I'm misunderstanding the component lifecycle (strongly possible), but it appears that once in a while, the "new" state is just dumped into _pendingState, in which case _performComponentUpdate() isn't called in that particular call to replaceState.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

@petehunt petehunt merged commit 7a0f2d7 into facebook:master Jul 3, 2013
bvaughn added a commit to bvaughn/react that referenced this pull request Aug 13, 2019
Preserve state when switching tabs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants