Why is my automatic counter not working?

I’ve written an automatic counter with React that increments every 1000ms(1sec) on the click of a button starting from 0. Here is the link to the pen:

The app consists of a div to display the count and a button with a click handler: “handleClick”.

I’ve written 3 implementations for “handleClick” and while only implementation 3 works as expected, I’m unable to understand why implementations 1 & 2 are behaving erroneously.

Implementation 1 only increments once when the button is clicked for the first time on page load.

Implementation 2 works correctly on first click. However, it starts behaving incorrectly on each subsequent click.

I need help understanding why the first 2 implementations are not working.

Thanks
Regards,
Bhashwar Sengupta

1 Like

Hi @BhashwarSengupta

You can check this video Learn useState in 15 minutes

I think it will help you to understand what are you doing wrong.

I also recommend you check freeCodeCamp courses in Youtube and Jack Herrington YouTube channel.

I hope all this will help you. Keep the good work.

Happy coding!!

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