Why we need to pass a function to setState() method in ReactJS?

The reason behind for this is that, setState() is an asynchronous operation. React batches state changes for performance reasons, so the state may not change immediately after setState() is called. That means we should not rely on the current state when calling setState() since we can’t be sure what that state will be. The solution is to pass a function to setState() , with the previous state as an argument. By doing this we can avoid issues with the user getting the old state value on access due to the asynchronous nature of setState().