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 ruleP1universalStack: React
reacterror-handlingasyncloading-state
Wrap async fetch calls in try/catch and reset loading on failure
If an awaited fetch function can throw, wrap it so isLoading is reset and an error state is set; otherwise a throw leaves the UI stuck loading forever.
PR: hegnar-forum-web · org-mining-3rd-2026-06Created: Jun 18, 2026
Bad example
Old codets
| 1 | setIsLoading(true); |
| 2 | const data = await fetchFn(); // throws -> isLoading stuck true, no error shown |
| 3 | setItems(data); |
| 4 | setIsLoading(false); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | setIsLoading(true); |
| 2 | try { |
| 3 | const data = await fetchFn(); |
| 4 | setItems(data); |
| 5 | } catch (e) { |
| 6 | setIsError(true); |
| 7 | } finally { |
| 8 | setIsLoading(false); |
| 9 | } |
Explanation (EN)
Objašnjenje (HR)