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.

[15.6.2] setState callback does not fire

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I have the following piece of code in my fairly simple component:

this.setState({ value: nextValue }, callback);

It works perfectly in React 16, but not in 15.6.2. On 15.6.2, callback does not fire. I investigated and saw this in react.development.js:

this.updater.enqueueSetState(this, partialState, callback, 'setState');

which clearly marks callback as one of the accepted arguments of enqueueSetState. However, when we go to the function that’s being called, in ReactUpdateQueue.js:

enqueueSetState: function (publicInstance, partialState) {

now we see this argument is ommited. I don’t know if that helps, anyway, the callback is not fired whatever I try to do.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

Here’s a simple example for reproduction: https://codesandbox.io/s/2kz06l3p0

What is the expected behavior?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 15.6.1, 15.6.2 (not tested other 15.x) NOT React 16.x

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
wojtekmajcommented, Mar 13, 2018

I honestly have my mind blown. When I change the minimal reproduction ( https://codesandbox.io/s/2kz06l3p0 ) so that I copy the whole Calendar.jsx, straight from the newest version into the same code here, it all works ( https://codesandbox.io/s/ko3n899jr3 )

There is a difference though! When I call this.setState, the same very line, now this.setState is calling a different function!

Here’s the setState in the first example that DID NOT work:

obraz

Here’s the setState in the second example that DID work:

obraz

Hmm… After all, it looks like actually there are two different versions of react and somehow react-calendar library has its own version imported…? 😨

0reactions
wojtekmajcommented, Feb 12, 2019

Yes. Just make sure that you have matching react and react-dom versions installed, and that your dependency is not misconfigured to require a different, specific react version on its own.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React setstate method's callback is not fired - Stack Overflow
Everything is working fine in React 15 but once I upgraded the react version to 16, the set state's callback function is not...
Read more >
React setState callback function - Code Gino
We indeed called setState 3 times, but that does not mean the value of the state variable will update after calling setState ....
Read more >
React v16.0 – React Blog
Breaking changes · Calling setState with null no longer triggers an update. · Calling setState directly in render always causes an update.
Read more >
Provide callback to useState hook like setState - LinkedIn
setState allows a second parameter to be passed to it as a callback. The callback function is invoked whenever the state of the...
Read more >
react-addons-transition-group | Yarn - Package Manager
This was not previously the case. Regardless, you should not be calling setState from render. setState callback (second argument) now fires immediately after ......
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 Hackernoon Post

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