Created
September 20, 2021 09:34
-
-
Save AbePlays/115736f0d967d26d0986d2ddabe024cb to your computer and use it in GitHub Desktop.
Custom Image Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ImgWithFallback = ({ | |
src: string, | |
fallback: string, | |
type = 'image/webp', | |
...props | |
}) => { | |
return ( | |
<picture> | |
<source srcSet={src} type={type} /> | |
<img src={fallback} {...props} /> | |
</picture> | |
); | |
}; | |
// Usage | |
/* | |
<ImgWithFallback | |
src="/images/me.webp" | |
fallback="/images/me.png" | |
alt="A photo of me" | |
/> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment