What is a closure in JavaScript? Give an example.
A closure is a function bundled with the scope it was created in, so it keeps access to those variables after the outer function returns. In React it explains stale state: an effect or callback captures the value of state from the render in which it was created, so an interval set up once keeps reading the initial value. The fixes are a functional state update or adding the dependency to the effect.