1

I am dynamically appending scripts to my react app like this

    export const appendScript = (scriptToAppend : string) => {
    const script = document.createElement("script");
    script.src = scriptToAppend;
    script.async = true;
    script.type = "text/jsx"; 
    document.body.appendChild(script);
}

App Component

class App extends React.Component {

  componentDidMount() {   
     appendScript("./assets/custom.min.js");
  } 
}

custom.min.js file

$(document).ready(function(){
    alert("ready");
})
1
  • You are mixing up multiple things. if you're having Single Page Application then open up the index.html and add the script after the body tag OR defer the execution of this javascript. React is for creating reusable components, understand the purpose. Commented Feb 26, 2022 at 13:51

2 Answers 2

1

Check the answer in https://stackoverflow.com/a/34425083/13558764.

Or you can try "react-helmet" package.

Here is a sample usage.

import React from "react";
import {Helmet} from "react-helmet";

class Application extends React.Component {
  render () {
    return (
        <div className="application">
            <Helmet>
                <script src="https://use.typekit.net/foobar.js"></script>
                <script>try{Typekit.load({ async: true });}catch(e){}</script>
            </Helmet>
            
        </div>
    );
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

I think react-helmet make it easy for beginners, also makes it easy to add multiple js files.
Loading JavaScript Files The problem with this approach is that it's hard to control when a particular script got loaded. Errors during loading are hard to process as well. For dynamically loading JavaScript files we may use the Code Splitting and React.lazy. For server-side rendering, we may use Loadable Components. Refer to this Article
0
import React, {useEffect} from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import HomePage from "./Components/Home/HomePage";

function App(){

useEffect(() => {
const script = document.createElement("script");
script.src = "/js/main.js"; //your path to your file inside your public folder
script.async = true;
document.body.appendChild(script);
return () => {
  document.body.removeChild(script);
};

}, []);` return( {/* this should work */} <Route path="/" element={} /> ) }

3 Comments

just do this in your app.js file and it going to work like a magic 🤝
Welcome to StackOverflow! Please fix your formatting and explain your code.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.