el.xwx.moe/components/ImageWithFallback.tsx

28 lines
483 B
TypeScript
Raw Normal View History

2023-05-27 14:05:07 -05:00
import { ReactNode, useState } from "react";
2023-05-25 09:17:20 -05:00
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;