Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Make effect setup and cleanup safe to repeat
Make effects safe under setup-cleanup-setup; do not hide Strict Mode behavior with a run-once flag.
Bad example
| 1 | useEffect(() => { |
| 2 | externalSdk.register(widgetId); |
| 3 | }, [widgetId]); |
Explanation (EN)
Registering without its matching teardown leaves an extra active registration after a repeated setup.
Objašnjenje (HR)
Registracija bez odgovarajućeg cleanupa ostavlja dodatnu aktivnu registraciju nakon ponovljenog setupa.
Good example
| 1 | useEffect(() => { |
| 2 | externalSdk.register(widgetId); |
| 3 | return () => externalSdk.unregister(widgetId); |
| 4 | }, [widgetId]); |
Explanation (EN)
For a reversible SDK registration, cleanup removes the previous registration before the next setup. Verify the SDK's actual unregister contract.
Objašnjenje (HR)
Za reverzibilnu SDK registraciju cleanup uklanja prethodnu registraciju prije sljedećeg setupa. Provjeri stvarni unregister ugovor SDK-a.
Notes (EN)
Make setup and teardown symmetrical so a setup-cleanup-setup sequence leaves one working registration. Test final unmount and input changes as well.
Bilješke (HR)
Setup i cleanup trebaju biti usklađeni tako da setup-cleanup-setup ostavi jednu aktivnu registraciju. Testiraj i konačni unmount te promjene ulaza.
Exceptions / Tradeoffs (EN)
Do not suppress the second setup with a ref after the first cleanup removed the resource. An irreversible external operation needs deduplication at its owner or an event boundary, not a fake unmount detector.