Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Don't register event listeners that can't fire or duplicate existing work
Drop listeners for lifecycle events that have already passed (load/DOMContentLoaded inside an effect) or that just repeat an initial check; keep only listeners for real future changes.
Bad example
| 1 | useEffect(() => { |
| 2 | check(); |
| 3 | // these fire on real changes |
| 4 | window.addEventListener('scroll', check); |
| 5 | window.addEventListener('resize', check); |
| 6 | // 'load' already fired before this effect ran -> never fires |
| 7 | window.addEventListener('load', check); |
| 8 | }, []); |
Explanation (EN)
By the time the effect runs, the page has loaded, so the `load` listener can never fire and only adds noise and a cleanup obligation for nothing.
Objašnjenje (HR)
Dok se efekt izvrsi, stranica je vec ucitana, pa se `load` listener nikad ne moze okinuti i samo dodaje sum i obvezu ciscenja ni za sto.
Good example
| 1 | useEffect(() => { |
| 2 | check(); |
| 3 | window.addEventListener('scroll', check); |
| 4 | window.addEventListener('resize', check); // layout can change on resize |
| 5 | }, []); |
Explanation (EN)
Only listeners that react to genuine future events remain, so every listener earns its place and cleanup stays minimal.
Objašnjenje (HR)
Ostaju samo listeneri koji reagiraju na stvarne buduce dogadaje, pa svaki listener opravdava svoje mjesto, a ciscenje ostaje minimalno.
Exceptions / Tradeoffs (EN)
Events like `pageshow` (bfcache restore) or `visibilitychange` can legitimately fire after mount and may be worth keeping when there is a concrete scenario; justify them rather than adding speculatively.
Iznimke / Tradeoffi (HR)
Dogadaji poput `pageshow` (bfcache obnova) ili `visibilitychange` mogu se legitimno okinuti nakon montiranja i vrijedi ih zadrzati kad postoji konkretan scenarij; opravdaj ih umjesto da ih dodajes spekulativno.