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

175
Visualizações
What can I use instead of the JavaScript Function constructor to get my parsed function executed in React app

From api I get this HTML:

<div class="my-class" data-src="/event/66478667"></div>
<script>(function(d, s, id) {var js,ijs=d.getElementsByTagName(s)[0];if(d.getElementById(id))return;js=d.createElement(s);js.id=id;js.src="https://embed.widget.js";ijs.parentNode.insertBefore(js, ijs);}(document, 'script', 'my-js'));</script>

Now I am parsing this HTML through react-html-parser package like:

import parser, { Transform } from 'react-html-parser';

 const transform: Transform = (node: { type: string; children: { data: string }[] }) => {
    if (node.type === 'script' && node.children?.length) {
      // eslint-disable-next-line no-new-func
      Function(node.children[0].data)();
    }
  };

  const options = {
    transform,
  };

  return (
    <div>
      {parser(html, options)}
    </div>
  );

The HTML is containing a <script> with a function. But this <script> tag doesn't get executed in the React jsx, after parsed and rendered. The only way to get the widget to work, is when I use the JavaScript Function constructor (with an IIFE).

From Eslint I get: The Function constructor is eval.eslintno-new-func.

Is there maybe a better / more safe way to get this function executed in my React app, instead of using the JavaScript Function constructor?

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