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

226
Views
React, how to add external script and make its functions available?

I wonder how I would add an external script to an React application and make its functions available. Let's take this script https://www.cssscript.com/confetti-falling-animation/

In a "not-react-application" I would add it like this in my DOM

<script src="confetti.js"></script>

and then call it's functions like this

startConfetti();

or

stopConfetti();

However, this does not work in React. I know that I can add a <script /> tag like this:

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

    script.src = './confetti.js';
    script.async = true;

    document.body.appendChild(script);

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

But this does not make the functions startConfetti() or stopConfetti() available. They are undefined.

How would I add the script and its functionalities in a React App?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Add the script in index.html's head tag, like so (with the correct path to the JavaScript file):

<script src="confetti.js"></script>

Then in your React component, you could get it with the help of window object, this way:

const startConfetti = window.startConfetti;
startConfetti();
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!