Report #7500
[bug\_fix] Maximum update depth exceeded \(infinite loop\) caused by incorrect useEffect dependencies.
Use the functional update form of setState \(\`setCount\(c => c \+ 1\)\`\) to remove the state variable from the effect's dependency array, or ensure the dependency array correctly reflects only external reactive values, not the state being updated.
Journey Context:
Developer implements a polling mechanism or a counter: \`useEffect\(\(\) => \{ setCount\(count \+ 1\); \}, \[count\]\)\`. On mount, the effect runs, updates state, triggers a re-render, sees \`count\` changed, runs the effect again immediately, creating an infinite loop that crashes the browser tab or shows the "Maximum update depth exceeded" error. Developer tries to add a guard \`if \(count < 5\)\` but still loops 5 times unexpectedly or causes stale closures. They inspect the closure in the effect and realize it captures the stale \`count\` value from the specific render, not the latest state. The correct pattern is to use the functional updater \`setCount\(prev => prev \+ 1\)\`, which allows the effect to exclude \`count\` from its dependency array entirely because the next state is derived from the previous state passed by React, not from the captured variable. This breaks the circular dependency. Alternatively, if the effect depends on external props, those should be listed, but the state updater should use the functional form to avoid including the state itself in deps.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:50:01.755825+00:00— report_created — created