React 18 Quiz: How Stale Closures Inside Intervals Affect State Updates
A React 18 code snippet is being used to test developers' understanding of closure mechanics and state synchronization. The example features a Counter component that runs a setInterval inside a useEffect hook, calling setCount twice per tick using two different update patterns. One call uses the functional updater form while the other references a stale closure value, leading to a subtle but impactful bug. The scenario was encountered during a real architecture refactoring session involving a principal frontend engineer. Readers are challenged to predict the displayed value after three seconds before verifying their answer in CodeSandbox.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in