Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Apply changes to the HTML strings when it comes from props in React?

I'm trying to add pre-loader icon before rendering an image in React component using the below code:

export default class MediaPostItem extends BaseComponent {

       public constructor(props: IMediaPostItemProperties) {
             super(props);
             this.state = {
                imageIsReady: false,
              };
        }

       componentDidMount(): void {
            const img = new Image();
            img.onload = () => this.setState({ imageIsReady: true });
            img.src = this.props.item.featuredImage?.node.sourceUrl;
       }
       
       public render(): ReactNode {
            const { item } = this.props;
            const { imageIsReady } = this.state;
            return( 
              {imageIsReady ? (
                 <img  src={item.featuredImage?.node.sourceUrl}/>
               ) : (<div> Loading</div>)
              })
       }
 }

Now in the other component I give a HTML strings as a real HTML in a react component using the below code:

 <div dangerouslySetInnerHTML={{ __html: props.content }}></div>

The content is including some <img> tags and what I need is to add a pre-loader for these img tags too. I'm not sure if it's possible or not. Is there any way to apply changes to the HTML strings when it comes from props?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do that with ReactDOM.render.

import { render } from "react-dom";

const htmlString = `
<div>
<div id="toBeReplaced"/></div>
</div>
`;

class ReplacedComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <h1>Replaced Component</h1>;
  }
}

export default class Parent extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    render(
      <React.StrictMode>
        <ReplacedComponent />
      </React.StrictMode>,
      document.getElementById("toBeReplaced")
    );
  }

  render() {
    return <div dangerouslySetInnerHTML={{ __html: htmlString }}></div>;
  }
}

You would need to select the <img> tags, with some attribute (class/id) then loop over them and render your component.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda