Hello,
I have a question about some behind-the-scenes details, if someone could please enlighten me.
Link to the challenge here.
Instead of simply passing an object as an argument to setState()
, we are told we need to pass a function that returns an object.
It talks about how setState()
is asynchronous, and if we don’t pass a function, then the setState()
method won’t receive the most updated values.
Is this because of the event loop? Because setState()
is asynchronous, a subsequent toggle call might return the state before it has updated, whereas if we return the object in a function, then a function will be queued in the event loop and wait for all the previous, asynchronous calls to setState()
to complete before the function executes in the event loop?
Thank you.