Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP1stack specificStack: react
reactuseEffectcleanupbug
Don't call setState in an effect cleanup function
Setting state in a useEffect cleanup causes redundant re-renders or infinite loops; remove the cleanup if there is nothing to actually clean up (or handle an abort signal instead).
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codetsx
| 1 | useEffect(() => { |
| 2 | fetchThread(threadId); |
| 3 | return () => { |
| 4 | setThread(null); // setState in cleanup -> extra renders / loops |
| 5 | }; |
| 6 | }, [threadId]); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 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)