Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically insert script tag that will execute via Reactjs

There are a bunch of answers on this, but I'm looking for something specific to reactjs

My component code:

  render: function () {

    return (
      <Modal {...this.props} title="Embed on your own site!">
        <div className="modal-body">
          <div className="tm-embed-container" dangerouslySetInnerHTML={{__html: embedCode}}>
          </div>
          <textarea className="tm-embed-code" rows="4" wrap="on" defaultValue={embedCode}></textarea>
        </div>
      </Modal>
    );
  }
});

The script tag is there on the page, but no execution. Should I go outside of react and just use good ol' DOM scripting as the other answers indicate?

like image 767
Kelly Milligan Avatar asked Dec 17 '14 16:12

Kelly Milligan


People also ask

How do I add a script dynamically in React?

We start by creating an empty <script></script> tag in the memory as script and then assign the necessary attributes to its src and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load this.

How do I add a script tag to React-helmet?

Adding a new script tag and directly appending it to the <head> element of the page is the easiest way to add <script> tags in the React app. react-helmet is a third party library that can be used to achieve the same thing by handling the <head> tag on every page.

Where do I put JavaScript code in React?

You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2 , user. firstName , or formatName(user) are all valid JavaScript expressions. In the example below, we embed the result of calling a JavaScript function, formatName(user) , into an <h1> element.

How do I add external script tags programmatically in react?

Loading external scripts in your React application can be as easy as adding a script tag to the HTML file that loads your React bundle; but this isn't always easy or possible. In this article you will learn how to add script tags programmatically, using JavaScript and standard DOM APIs. Create a new element of type script. Set its src attribute.

How to include JavaScript inside a react application?

In this article, we will see different ways to include JavaScript inside a react application. If you want the script to be loaded on every page of your application, you can directly add it to the index.html as shown below: 1... 8... If you run the application, inspect and check, you will see the script added inside the head tag:

How to load a script dynamically in react?

Another way is to load the script is to load it dynamically. You can load script on that page on which is it really required You can have a fallback UI till the script is being loaded. It will improve make the user experience better. Since our website is in React, I am going to use hooks for loading script dynamically.

How do you create a dynamic script in HTML?

If we don’t find an existingScript; we create the script dynamically. We start by creating an empty <script></script> tag in the memory as script and then assign the necessary attributes to its src and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load this.


1 Answers

For what I understand, the react way would be to use the "componentDidMount" function to insert what you want in the the div, including some external script.

If your component is dynamic and receive regulary new props, you should also consider other methods (like componentDidUpdate).

More about script insertion with jQuery on this question

  componentDidMount: function(){
    var embedCode = this.props.embedCode; // <script>//my js script code</script>
    
    $('#scriptContainer').append(embedCode);
    },

  render: function () {
    return (
      <Modal {...this.props} title="Embed on your own site!">
        <div className="modal-body">
          <div className="tm-embed-container" id="scriptContainer">
          </div>
          <textarea className="tm-embed-code" rows="4" wrap="on" defaultValue={this.props.embedCode}></textarea>
        </div>
      </Modal>
    );
  }
});
like image 106
Bactisme Avatar answered Oct 21 '22 03:10

Bactisme