Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Do not display image if link is broken

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

enter image description here

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...

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try something like this (not tested), using onLoadingComplete and style prop

const [isImageLoaded, setIsImageLoaded] = useState(false);

   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' 
             /* set style based on load status */
             style={{...(isImageLoaded ? {} : {display:'none'}) }
                   } 
             /* set flag after image loading is complete */
             onLoadingComplete={()=>{ setIsImageLoaded(true); }}>
          </Image>
       </div>
about 4 years ago · Juan Pablo Isaza Relatório

0

One of the thing you can do is to define the state for hidng or displaying the image like this:

const [displayimage, setDisplayimage] = useState(true);

Then you can use it like this

    return <div className="overflow-hidden ">
       <Image className={`relative w-full h-40 object-cover rounded-t-md ${displayimage ? `block`: `hidden`} `  src={cover_url} alt={data.name} width=`${displayimage ? 600: 0}` height=`${displayimage ? 400: 0}`  onError={() => setDisplayimage(false)}></Image>
           </div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda