Report #53713
[bug\_fix] Infinite loop in useEffect due to missing or incorrect dependency
Include all reactive values \(props, state, functions\) used inside useEffect in the dependency array, or use the functional update form of setState \(e.g., \`setCount\(c => c \+ 1\)\`\) to avoid needing the current state in dependencies. If the effect should run once on mount, ensure the dependency array is \`\[\]\` and all used values are either outside the component or defined with \`useCallback\`/\`useRef\` to be stable. Root cause: ESLint react-hooks/exhaustive-deps warns or the effect re-runs because a dependency changes, causing a state update that changes the same dependency, creating a cycle.
Journey Context:
Developer writes \`useEffect\(\(\) => \{ fetchData\(query\).then\(setData\); \}, \[\]\);\` but sees a lint warning that \`query\` is missing from dependencies. They add \`query\` to the array: \`\[query\]\`. However, \`query\` is a state variable that changes on every keystroke. This causes the effect to run continuously, fetching data in a loop and potentially freezing the browser. They realize they need to debounce the query or separate the fetch logic. They learn that the dependency array must include all reactive values, but if that causes a loop, they need to restructure: either use a ref for the previous value, use the functional setState if updating based on previous state, or ensure the dependency is stable \(e.g., via useCallback\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T20:39:06.640876+00:00— report_created — created