Report #104258
[bug\_fix] React useEffect infinite loop or stale closure due to missing dependencies in the dependency array
Include all reactive values \(props, state, derived values\) used inside the effect in the dependency array. If you intentionally want to run the effect only once, pass an empty array \`\[\]\` only if the effect uses no reactive values. Use \`useCallback\` or \`useMemo\` to stabilize function dependencies.
Journey Context:
I had a \`useEffect\` that fetched data based on a \`userId\` prop. I wrote \`useEffect\(\(\) => \{ fetchUser\(userId\); \}, \[\]\);\` expecting it to run once, but the \`userId\` was undefined on first render and then updated later. The effect never re-ran, so data never loaded. I added \`userId\` to the deps, but then I got an infinite loop because I was also calling \`setState\` inside the effect without a condition. I removed the state setter from the effect and used a separate effect for side effects. The real root cause: React's exhaustive-deps lint rule is not just a style guide — it prevents bugs. I enabled the \`react-hooks/exhaustive-deps\` ESLint plugin and fixed all warnings. Now I always include every variable from the component scope that is used inside the effect.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-19T20:07:59.816798+00:00— report_created — created