React - Use State to Toggle an Element

Sometimes you might need to know the previous state when updating the state. However, state updates may be asynchronous - this means React may batch multiple setState() calls into a single update

I’m having really hard time understanding this. Can anyone give me an example of how react may batch multiple setState calls into a single update?

Hello there. I don’t know if I have a perfect example to explain, but I did a bit of reading and I can (kind of) see what it means.

State updates are async because they alter state and cause re-rendering, so pending states are created instead. They are all batched and updated together to kind of reduce the cost of operations. Because imagine you had many states in your app that needed updating and state updates were synchronous. During execution, you get so many re-renders.

I don’t know if my breakdown makes sense, but you can check this answer

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.