Skip to content

Commit

Permalink
fix(images): fix background images
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmen committed Dec 3, 2023
1 parent 895ece9 commit 1e8252f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/images/BackgroundImage/BackgroundImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface BackgroundImageProps extends StyleProps {

export const BackgroundImage: FC<BackgroundImageProps> = ({src, empty, className, style, children}) => {
const [error, setError] = useState(false);
const [load, setLoad] = useState<boolean>()
const [load, setLoad] = useState<boolean>();
const [_src, setSrc] = useState<string | null>(null);

useEffect(() => {
Expand All @@ -20,17 +20,22 @@ export const BackgroundImage: FC<BackgroundImageProps> = ({src, empty, className
}, [src]);

useEffect(() => {
const img = new Image()
img.src = src
img.onload = () => {
setLoad(false);
setSrc(src);
};
img.onerror = () => {
if (src) {
const img = new Image();
img.src = src;
img.onload = () => {
setLoad(false);
setSrc(src);
};
img.onerror = () => {
setError(true);
setLoad(false);
};
} else {
setError(true);
setLoad(false);
}
}, [src])
}, [src]);

return <Box
className={className}
Expand Down

0 comments on commit 1e8252f

Please sign in to comment.