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

114
Visualizações
Handle onError image on mapping React

I have this images iterating, and only on error of image I have to add a background color to the parent element div. So my code is like this

 
import React, { useState, useEffect } from 'react'
import COLORS from 'core/colors/colors'
...

export const ComponentSample: React.FC = () => {
 
 const [imagePlaceholder, setImagePlaceholder] = useState(false)
  const addDefaultSrc = (ev) => {
   
    if (!ev.currentTarget.onerror) {
      setImagePlaceholder(true)
    }
    ev.target.src = 'https://socialistmodernism.com/wp-content/uploads/2017/07/placeholder-image.png?w=640'
    ev.currentTarget.className = styles.placeholderImg;
  }
  console.log(imagePlaceholder)
  const colors = Object.values(COLORS)
  const randomColor = imagePlaceholder ? colors[Math.floor(Math.random() * colors.length)] : COLORS.white

 return <div className={styles.container}>
 <div className={styles.imageContainer} key={index} style={{ backgroundColor: randomColor }} >
                      <img src={project.thumbnailUrl} onError={addDefaultSrc} className={styles.imagePost} />
 
 </div>
 
 }

The problem here is that imagePlaceholder is becoming true and always returning true, I don't know how to make only on image that has error

about 4 years ago · Juan Pablo Isaza
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