79207556

Date: 2024-11-20 14:13:13
Score: 1
Natty:
Report link

States in React are asynchronous and the value will only update after the function has finished running. Since your function never stops running (as it loops the setTimeout), the asynchronous call never gets a chance to finish and update the state value, thus it remains at 1. For this reason, state is not the best way to do what you are currently trying to do. Define counter using let outside of the scope of TestComponent instead.

Reasons:
  • Has code block (-0.5):
  • Single line (0.5):
  • Low reputation (1):
Posted by: Christiaan Prinsloo