el.xwx.moe/components/ImageWithFallback.tsx
2023-05-27 22:35:07 +03:30

28 lines
483 B
TypeScript

import { ReactNode, useState } from "react";
type Props = {
src: string;
className: string;
children: ReactNode;
};
const ImageWithFallback = ({ src, className, children, ...rest }: Props) => {
const [error, setError] = useState(false);
return error ? (
<>{children}</>
) : (
<img
alt=""
{...rest}
src={src}
className={className}
onError={() => {
setError(true);
}}
/>
);
};
export default ImageWithFallback;