I created a component named Avatar
and it wraps Image
of Next.JS.
The parent component of Avatar
uses src
prop (which is a string) to change the image source (this will be a remote source). If it does not specify the src
prop, then a default local image will be used as shown in the following code snippet.
import Image from "next/image";
import styles from "./Avatar.module.css";
import AvatarPhoto from "../images/avatar-default.png";
export const Avatar = ({ src, alt, size = 47 }) => {
return (
<div className={styles.imgContainer}>
<Image
className={styles.img}
src={src ?? AvatarPhoto}
alt={alt}
width={size}
height={size}
/>
</div>
);
};
What I could not achieved is if the specified source is invalid, how can I show the local image. For example; if there is not a image in the specified source (If the Get request for the source results with 404 Error), I want to use the default avatar image.
Is there a fallback property of Next's Image
?
If not, how can I do that ?