Code QuizIntermediate

Stale Closure in Updates

A setInterval captures a stale state value.

Codejavascript
function Timer() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    const id = setInterval(() => {
      setCount(count + 1);
    }, 1000);
    return () => clearInterval(id);
  }, []);

  return <Text>{count}</Text>;
}

What is the bug in this code?