1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-18 21:09:40 +02:00
portainer/app/react/components/FallbackImage.tsx

31 lines
693 B
TypeScript

import { ReactNode, useEffect, useState } from 'react';
interface Props {
// props for the image to load
src?: string; // a link to an external image
fallbackIcon: ReactNode;
alt?: string;
className?: string;
}
export function FallbackImage({ src, fallbackIcon, alt, className }: Props) {
const [error, setError] = useState(false);
useEffect(() => {
setError(false);
}, [src]);
if (!error && src) {
return (
<img
className={className}
src={src}
alt={alt}
onError={() => setError(true)}
/>
);
}
// fallback icon if there is an error loading the image
return <div className={className}>{fallbackIcon}</div>;
}