All jobs / Frontend / web developer

Frontend / web developer interview question · 1–3 years

A setInterval inside useEffect always logs the initial count. Why?

The strongest answer

The effect's callback closed over the count from the render it was created in; either list count as a dependency and reset the interval, use a functional state update, or keep the latest value in a ref

Why it works

Right. Stale closure — the classic hooks trap.

What a good answer covers

Answers that fall short

Who gets asked this

More Frontend / web developer questions