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

100
Vistas
Adding a script with a React hook

I'm trying to add inline scripting to a React component: I have my custom hook:

import { useEffect } from 'react';

const useScript = url => {
  useEffect(() => {
    const script = document.createElement('script');

    script.src = url;
    script.async = true;

    document.body.appendChild(script);

    return () => {
      document.body.removeChild(script);
    }
  }, [url]);
};

export default useScript;

Which I use like so:

import useScript from 'hooks/useScript';

const MyComponent = () => {
  useScript('path/to/my.js');
}

Script added to the document after SPA navigation event but it doesn't add events to my buttons without reloading the page.

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

0

I had a problem inside one of the added scripts. I changed the way of adding script. I exported the function inside js, then I imported this function inside my component and then I used useEffect.

import { MyFunction } from './myscript.js';

const MyComponent = () => {
    
    useEffect(() => {
        MyFunction();              
     }, []);
    
    return(
       ......
    )
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