Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Avoid unnecessary state resets in effect cleanup
Avoid unnecessary local state resets in effect cleanup; preserve intentional reset behavior and cancel or ignore stale work.
Bad example
| 1 | useEffect(() => { |
| 2 | fetchThread(threadId); |
| 3 | return () => setThread(null); |
| 4 | }, [threadId]); |
Explanation (EN)
Resetting local result state during cleanup can add an unnecessary transitional render and does not cancel an in-flight request.
Objašnjenje (HR)
Good example
| 1 | useEffect(() => { |
| 2 | const controller = new AbortController(); |
| 3 | fetchThread(threadId, controller.signal); |
| 4 | return () => controller.abort(); // clean up the request, don't setState |
| 5 | }, [threadId]); |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Use cleanup to undo the previous effect's external work. Establish how UI state resets when inputs change before removing a setter.
Exceptions / Tradeoffs (EN)
Cleanup also runs before a dependency-driven replacement, not only on unmount. A state update is not inherently an infinite loop; keep deliberate shared-owner synchronization if its contract requires it.