• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

109
Vistas
Execute javascript code inside script-tag in React jsx

I am adding this script-tag to my React jsx render:

<script>alert('Hello world')</script>

Why is the Javascript code <script>alert('Hello world')</script> not executing in my React app?

Update:

I am debugging this because I get this HTML back from api:

<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 html-react-parser package like:

return <div data-testid="html-paragraph">{parser(html)}</div>;

But this isn't working because the Javascript code isn't executing?

How to solve this?

See here my code sandbox.

over 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Remove the <script> tag and replace with {}.

So: {alert('Hello world')}

over 3 years ago · Juan Pablo Isaza Denunciar

0

You can add it via npm package. I used it as ( once ):

import React, { Component } from 'react';
import ScriptTag from 'react-script-tag';
 
class Demo extends Component {
 
    render() {
        return (<ScriptTag isHydrating={true} type="text/javascript" src="some_script.js" />);
    }
}

Here is the npm package link https://www.npmjs.com/package/react-script-tag

over 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda