<div className="bg-[#F8F8F8] flex items-center justify-center px-2 py-10">
<Image src={image} alt="course image" width={100} height={100} />
</div>
I want when image is not available due to error so replace it with dummy image how can i do that?
2
Answers
import fallbackImage from
./assets/dummy-image.jpg;
import the fallback image and Conditional src:
The src attribute uses the original image. If the original image is unavailable or fails to load, the src event handler sets the src to the fallback image.
To replace a failing image with a dummy image in a React component, you can use the
onError
event on theImage
tag to handle image loading errors. Here’s a simplified version:Key Points:
useState
to manage the image URL.onError
callback to switch to a dummy image if the original fails to load.