postio / libraries /react-shared-libraries /src /helpers /image.with.fallback.tsx
Leon4gr45's picture
Upload folder using huggingface_hub (part 11)
f2e9a59 verified
Raw
History Blame Contribute Delete
624 Bytes
import { FC, useEffect, useState } from 'react';
import SafeImage from './safe.image';
interface ImageSrc {
src: string;
fallbackSrc: string;
width: number;
height: number;
[key: string]: any;
}
const ImageWithFallback: FC<ImageSrc> = (props) => {
const { src, fallbackSrc, ...rest } = props;
const [imgSrc, setImgSrc] = useState(src);
useEffect(() => {
if (src !== imgSrc) {
setImgSrc(src);
}
}, [src]);
return (
<SafeImage
alt=""
{...rest}
src={imgSrc}
onError={() => {
setImgSrc(fallbackSrc);
}}
/>
);
};
export default ImageWithFallback;