I'm returning some images dynamically
return <div className="overflow-hidden ">
<Image className="relative w-full h-40 object-cover rounded-t-md" src={cover_url} alt={data.name} width='600' height='400' onError={e => e.target.style.display = 'none'}></Image>
</div>
Some of the links will certainly be broken, in these situations the image should not appear.
To try to do this I'm using onError
The problem is that the image space is still there. In case the link is broken, there should be no empty space, I thought a display='none' would be enough. I need it just not to exist. Is there any way?
(I'm using nextjs and next/image)
Edit: Another problem with this onError method, is that the broken image icon seems to flash quickly on the screen before disappearing.
Maybe it's necessary not to return the Image
tag for the broken links instead of hiding it, but I still don't know how...