setInterval callback reads stale state every tick
React 17.0.2, plain CRA. I set up a setInterval(() => setCount(count + 1), 1000) inside a useEffect with []. count never goes past 1. I know the empty deps is the problem but if I add [count] the interval keeps getting torn down and recreated and it fires weird. What's the actual fix here without pulling in some library?
3 replies