Last active
November 17, 2021 09:58
-
-
Save codegino/b9ed69fec8495c5024ef925c432bf1f2 to your computer and use it in GitHub Desktop.
Next Image with placeholder and error handler
This file contains hidden or 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
| function CustomImage({...props}: ImageProps) { | |
| const [src, setSrc] = React.useState(props.src); | |
| return ( | |
| <Image | |
| onError={() => setSrc('/assets/image-error.png')} | |
| placeholder="blur" | |
| blurDataURL="/assets/image-placeholder.png" | |
| {...props} | |
| src={src} | |
| /> | |
| ); | |
| } |
This file contains hidden or 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
| import Image, {ImageProps} from 'next/image'; | |
| function CustomImage({alt, ...props}: ImageProps) { | |
| const [src, setSrc] = React.useState(props.src); | |
| return ( | |
| <Image | |
| {...props} | |
| src={src} | |
| alt={alt} | |
| onError={() => setSrc('/assets/image-error.png')} | |
| placeholder="blur" | |
| blurDataURL="/assets/image-placeholder.png" | |
| /> | |
| ); | |
| } |
This file contains hidden or 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
| import React from 'react'; | |
| import { CustomImage } from '../components/CustomImage'; | |
| export default function IndexPage() { | |
| return ( | |
| <CustomImage | |
| src="https://i.imgur.com/gf3TZMr.jpeg" | |
| layout="fixed" | |
| height={500} | |
| width={700} | |
| alt={'alt'} | |
| /> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment