11import React from "react" ;
2- import { ImageProps } from "next/image" ;
2+ import Image , { ImageProps , StaticImageData } from "next/image" ;
33import styled , { css } from "styled-components" ;
44
55interface FallbackImgImageProps extends ImageProps {
6- fallbackSrc : string ;
6+ fallbackSrc : string | StaticImageData ;
77 alt : string ;
88 sizes ?: string ;
99 rounded ?: boolean ;
@@ -20,19 +20,24 @@ const FallbackImgImage = ({
2020 rounded,
2121 ...props
2222} : FallbackImgImageProps ) => {
23- const [ imgSrc , setImgSrc ] = React . useState < string > ( "" ) ;
23+ const [ isError , setIsError ] = React . useState ( false ) ;
2424
25- React . useEffect ( ( ) => {
26- setImgSrc ( src ) ;
27- } , [ src ] ) ;
28-
29- return (
25+ return isError ? (
3026 < StyledImage
31- src = { imgSrc }
27+ src = { src }
28+ { ...props }
29+ alt = { alt }
30+ isshouldhide = { isShouldHide }
31+ onError = { ( ) => setIsError ( true ) }
32+ isrounded = { rounded ?. toString ( ) }
33+ />
34+ ) : (
35+ < StyledFallbackImage
36+ src = { fallbackSrc }
3237 { ...props }
3338 alt = { alt }
3439 isshouldhide = { isShouldHide }
35- onError = { ( ) => setImgSrc ( fallbackSrc ) }
40+ onError = { ( ) => setIsError ( true ) }
3641 isrounded = { rounded ?. toString ( ) }
3742 />
3843 ) ;
@@ -55,4 +60,21 @@ const StyledImage = styled.img<{
5560 ` }
5661` ;
5762
63+ const StyledFallbackImage = styled ( Image ) < {
64+ isshouldhide ?: boolean ;
65+ isrounded ?: string ;
66+ } > `
67+ height: auto;
68+ ${ ( { isshouldhide } ) =>
69+ isshouldhide &&
70+ css `
71+ display : none;
72+ ` }
73+ ${ ( { isrounded } ) =>
74+ isrounded === "true" &&
75+ css `
76+ border-radius : 50% ;
77+ ` }
78+ ` ;
79+
5880export default FallbackImgImage ;
0 commit comments