Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Validate dependencies inside useEffect
Guard effects against invalid optional inputs using domain validation, not unconditional truthiness checks.
Bad example
| 1 | useEffect(() => { |
| 2 | // Triggers a network request even if userId is undefined, null, or empty |
| 3 | fetch(`/api/users/${userId}`) |
| 4 | .then((res) => res.json()) |
| 5 | .then(setUser); |
| 6 | }, [userId]); |
Explanation (EN)
An absent identifier can produce a request for an unintended path.
Objašnjenje (HR)
Nedostajući identifikator može poslati zahtjev na nenamjeravanu putanju.
Good example
| 1 | useEffect(() => { |
| 2 | if (userId == null || userId === "") return; |
| 3 | let active = true; |
| 4 | void loadValidatedUser(userId).then( |
| 5 | user => { if (active) setUser(user); }, |
| 6 | error => { if (active) reportError(error); }, |
| 7 | ); |
| 8 | return () => { active = false; }; |
| 9 | }, [userId]); |
Explanation (EN)
The imported loader validates the response; the effect preserves numeric zero and ignores a result after its input changes. The domain must define other invalid ID forms.
Objašnjenje (HR)
Uvezeni loader validira odgovor. Effect čuva brojčanu nulu i ignorira rezultat nakon promjene ulaza. Domena mora definirati ostale neispravne oblike ID-ja.
Notes (EN)
Validate optional or untrusted inputs against the actual domain before issuing dependent work; preserve valid zero values and define missing-input behavior.
Exceptions / Tradeoffs (EN)
Already validated required inputs do not need redundant guards. A truthiness check is not full validation, and fetch results still need status/schema and stale-request handling.