Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!