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

154
Visualizações
JavaScript not running when added with componentDidUpdate

I have two versions of a component that display a file using the Jsmol library. The first version works but will displays the file before getting user input. I want the Jsmol functions to run after getting user input so I put the functions inside componentDidUpdate. However, when I do this the Jsmol does not run at all. I am thinking it is to prevent cross-site scripting attacks.

This is the code for my componentDidMount (This runs the JavaScript in innerHTML):

componentDidMount() {
      const script = document.createElement("script");
      script.id = "display"
      script.type = 'text/JavaScript';
      script.innerHTML = "jmolInitialize('jmol-14.32.63/jsmol');\
                           jmolApplet(400, `load results.pdb`, 0);"
      this.instance.appendChild(script);
}
render() {
       return (
          <>
            <div id="display" ref={el => (this.instance = el)} />
          </>
       );
}

And this is another version of the code (This does not run the JS in innerHTML):

componentDidUpdate() {
      const script = document.getElementById("jsmol-script");
      script.innerHTML = "jmolApplet(400, `load results/1A6W.pdb`, 0);"
}

render() {
       return (
          <>
            <div id="display" ref={el => (this.instance = el)} >
               <script id="jsmol-script" type="JavaScript"></script>
            </div>
          </>
    );
}
about 4 years ago · Santiago Trujillo
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