Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Define effect-only helpers inside the effect to avoid useCallback
If a function is only used inside one effect, declare it inside that effect instead of wrapping it in useCallback and listing it as a dependency.
Bad example
| 1 | const handler = useCallback(() => doWork(), []); |
| 2 |
|
| 3 | useEffect(() => { |
| 4 | window.addEventListener('resize', handler); |
| 5 | return () => window.removeEventListener('resize', handler); |
| 6 | }, [handler]); |
Explanation (EN)
A useCallback wrapper adds a separate identity contract for a helper owned by one effect.
Objašnjenje (HR)
Good example
| 1 | useEffect(() => { |
| 2 | const handler = () => doWork(); |
| 3 | window.addEventListener("resize", handler); |
| 4 | return () => window.removeEventListener("resize", handler); |
| 5 | }, [doWork]); |
Explanation (EN)
Track doWork when it is reactive; an imported stable helper does not need a dependency.
Objašnjenje (HR)
Notes (EN)
Move a helper used only by one effect inside it when this clarifies ownership, then include all reactive values referenced by the helper.
Exceptions / Tradeoffs (EN)
Keep a shared helper outside when other callers need it. Moving its declaration does not make captured props nonreactive.