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 ruleP2stack specificStack: react
reactcomponentsimagesreuse
Build a reusable image-with-fallback component with onError handling
When a fallback image/initial pattern recurs, extract a component that renders a fallback and handles onError for broken sources.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetsx
| 1 | {src ? <img src={src} alt={alt} /> : <span>{alt[0]}</span>} // repeated everywhere |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | function ImageWithFallback({ src, alt }: Props) { |
| 2 | const [failed, setFailed] = useState(false); |
| 3 | if (!src || failed) return <span>{alt[0]}</span>; |
| 4 | return <img src={src} alt={alt} onError={() => setFailed(true)} />; |
| 5 | } |
Explanation (EN)
Objašnjenje (HR)