Fix the Stale Counter

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

A component shows a counter that should go up by one every second. It keeps the count in state, and an effect with an empty dependency list starts an interval that sets the count to the current count plus one. On screen, the counter goes from zero to one and then stays at one. Explain why, and how you'd fix it.

What a strong answer covers

  • Why the interval keeps seeing the first value.
  • What the effect's dependencies have to do with it.
  • Ways to update the count that don't depend on the stale value.
  • Cleaning up the interval.

Common mistake: Adding the count as a dependency but forgetting the cleanup, so intervals pile up.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud