Skip to content
Advertisement

Multiple useState in if statement

I’m trying to make a small tennis match simulator.

And so far everytime a player reach actualScore: 4 I would like to update my player useState and add +1 to their setWon, however it never update it and I don’t get why. The block of code:

JavaScript

All the code:

JavaScript

Advertisement

Answer

  1. useState is an asynchronus operation. So when you are trying to call multiple setState at the same time the execution is messed up.

  2. also you will need to set the data based on the previous data.

Here is how the code should be modified

JavaScript

and the updates should work correctly

Advertisement