Feat Widgets
This commit is contained in:
28
Elecciones-Web/frontend/src/components/ImageWithFallback.tsx
Normal file
28
Elecciones-Web/frontend/src/components/ImageWithFallback.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
// src/components/ImageWithFallback.tsx
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
interface Props extends React.ImgHTMLAttributes<HTMLImageElement> {
|
||||
fallbackSrc: string;
|
||||
}
|
||||
|
||||
export const ImageWithFallback = ({ src, fallbackSrc, ...props }: Props) => {
|
||||
const [imgSrc, setImgSrc] = useState(src);
|
||||
const [error, setError] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setError(false);
|
||||
setImgSrc(src);
|
||||
}, [src]);
|
||||
|
||||
const handleError = () => {
|
||||
setError(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<img
|
||||
src={error || !imgSrc ? fallbackSrc : imgSrc}
|
||||
onError={handleError}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user