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 ruleP1stack specificStack: react
react-querydata-fetchingstatemutations
Use React Query hooks for server data instead of manual fetch-in-handler
Fetch/mutate server state through useQuery/useMutation hooks rather than ad-hoc fetches in click handlers, so caching, loading and error states are handled consistently.
PR: hegnar-journalist-boost · org-mining-deep-2026-06Created: Jun 17, 2026
Bad example
Old codetsx
| 1 | const onShare = async () => { |
| 2 | setLoading(true); |
| 3 | const res = await fetch('/api/share', { method: 'POST' }); |
| 4 | setLoading(false); |
| 5 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | const { mutate: share, isPending } = useMutation({ mutationFn: shareArticle }); |
| 2 | const onShare = () => share(payload); |
Explanation (EN)
Objašnjenje (HR)