Scope and closures
Scope is which names a line of code can see. A name made inside a function or a block can only be seen there. A closure is a function that keeps hold of the variables around where it was created, even after that code has finished.
Agents write functions that get called later all the time, and closures explain a whole family of bugs where one of them uses an old value. If a button acts on out-of-date data, this is the first thing to suspect.
Say it to your agent
“This click handler logs an old count. I think its closure captured the value from an earlier render. Confirm that with a log first, then fix it using setCount((c) => c + 1). Do not add a ref.”