I think your actual problem is that the timer only updates once, right? Well thats because you lied to React about your dependencies, your effect uses count
, add that as an dependency:
useEffect(() => {
let id = setInterval(() => {
setCount(count + 1); // here you use count
}, 1000);
return () => clearInterval(id);
}, [count]); // here you have to tell react about your dependencies
Now the function returned from the effect will be called whenever count
changes, or when the component unmounts. So you could actually write your code as:
useEffect(() => {
let id = setTimeout(() => {
setCount(count + 1);
}, 1000);
return () => clearTimeout(id);
}, [count]);
This basically does: Whenever 'count' changes, shedule a timer to change 'count' again in 1 second.
However, using an interval that runs as long as the component is mounted would work too, but for that to work we need to (1) remove all dependencies (count
) and (2) get the latest count
somehow. Fortunately the state setter also takes a callback:
useEffect(() => {
let id = setInterval(() => setCount(count => count + 1), 1000);
return () => clearInterval(id);
}, []); // we don't lie anymore