tezvyn:

What is a stale closure in React hooks?

Source: dmitripavlutin.comadvanced

Tests closures and hook dependencies. A strong answer defines stale closure as capturing an outdated variable, shows a useEffect interval with stale state, and fixes it with dependencies and cleanup. Red flag: omitting cleanup or using refs blindly.

Tests deep understanding of JavaScript closures and React's data flow. A strong answer defines stale closure as a callback capturing a variable from an earlier render, demonstrates it with a useEffect interval that logs a stale count because the empty dependency array freezes the closure, and fixes it by including count in dependencies and returning a cleanup function to clear the interval. Red flag: suggesting useRef as a fix without explaining the mutation model, or claiming setState callbacks alone prevent the issue.

Read the original → dmitripavlutin.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

What is a stale closure in React hooks? · Tezvyn