Pending state updates may be confusing
See original GitHub issueIf you have
componentWillReceiveProps: function() {
this.setState({x: this.state.x + 1});
this.setState({x: this.state.x + 1});
}
then x
will only be incremented once because the new state will just be stored in _pendingState
until receiveProps
finishes. After my refactoring in #115, this will never increment twice, which is probably confusing.
Maybe this isn’t a bug. We can fix this behavior by instead updating this.state immediately (but still updating the UI later). Right now I can’t think of any situations where having this.state out of sync with the UI is a problem.
@vjeux suggested I open an issue, so here I am. Probably easiest for me to fix while updating #115 though.
Issue Analytics
- State:
- Created 10 years ago
- Comments:43 (11 by maintainers)
Top Results From Across the Web
setState doesn't update the state immediately - Stack Overflow
It means you can't call it on one line and assume the state has changed on the next. According to React docs. setState()...
Read more >Dive into React codebase: Handling state changes
updateComponent is called if there is any pending state. You may think why these checks for pending state or force updates are needed....
Read more >Solved: out of the box pending state - ServiceNow Community
Solved: i am looking to use the pending state for incident. The value on it shows -5 Does this have any significance?
Read more >Queueing a Series of State Updates - React Docs
To do this, it helps to understand how React batches state updates. ... However, as you might recall from the previous section, each...
Read more >F-1 Optional Practical Training (OPT) | Study in the States
(For example a student may have 12 months for a bachelor's degree and ... Note: Any OPT status updates to SEVIS are automatic...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It might be best to think of
setState
as beingenqueueStateUpdate
. With that understanding, much of that confusion seems to go away. I have been pondering the exact same thing as you - should we let you “read” the state updates immediately after you write them, even if the UI has not been synchronized with it? At first I thought we should let you read the update immediately. But then I realized something: Not allowing reads of state after enqueueing the updates has an interesting effect. It encourage you to aggregate the entire update payload into a single object and execute a singlesetState
. Since mutations are the hardest things in the world to understand, it makes sense to encourage squeezing them into small, centralized parts of your code. There’s a theory that it’s better to have all the crap in one pile, as opposed to spreading it out all over your code. Depending on how you feel about mutations, that stance might apply here. I haven’t thought enough about this to feel strongly in either way, but I thought I’d share my recent thoughts - I’ve gone back and forth on this one.Wrote some thoughts about why React works this way: https://github.com/facebook/react/issues/11527#issuecomment-360199710