question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pending state updates may be confusing

See original GitHub issue

If 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:closed
  • Created 10 years ago
  • Comments:43 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
jordwalkecommented, Jun 24, 2013

It might be best to think of setState as being enqueueStateUpdate. 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 single setState. 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.

3reactions
gaearoncommented, Jan 24, 2018

Wrote some thoughts about why React works this way: https://github.com/facebook/react/issues/11527#issuecomment-360199710

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found