Report #40901
[bug\_fix] useEffect runs with stale state or causes infinite re-render loop
Include all reactive values in dependency array, use functional setState updates, or use a ref for non-reactive values
Journey Context:
Developer implements search with useEffect fetching API when query changes. Writes \`useEffect\(\(\) => \{ fetch\('/api?q=' \+ query\) \}, \[\]\)\` with empty deps to avoid loops. Later adds pagination, includes \`page\` in deps but forgets \`query\`. Effect runs with stale \`query\` when page changes. Developer tries adding \`query\` to deps but now every keystroke triggers fetch causing race conditions. They implement abortController but still too many requests. Finally uses a ref to track latest query without triggering effect, or wraps fetch in useCallback with proper deps, or uses a debounce hook, understanding the closure capture mechanism.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T23:07:17.985677+00:00— report_created — created