While this wasn’t today exactly, I just learned this recently, and it made a difference for something I was working on.
I had been looking around for a way to determine if the component’s
state had changed as I expected, mostly doing some debugging and
tracing, I discovered that
setState is an asynchronous function. The
second parameter to
setState is a callback function that runs when
the state has finished updating.
In my case, doing a simplistic
console.log debugging thing, I ended
There are much better reasons for having that callback, of course, but this is the one I found a need for at the time.