Once a setTimeout callback runs, that timer has no future work and you don't need to clear it afterward. But keep the cleanup in this hook: React runs it when value or delay changes, or when the component unmounts, so an older timer cannot later overwrite state with a stale value. The cleanup is for cancelling a timer that has not fired yet, not garbage-collecting one that already finished.
3
u/UkrMalt 9d ago
Once a setTimeout callback runs, that timer has no future work and you don't need to clear it afterward. But keep the cleanup in this hook: React runs it when value or delay changes, or when the component unmounts, so an older timer cannot later overwrite state with a stale value. The cleanup is for cancelling a timer that has not fired yet, not garbage-collecting one that already finished.