4

I have a problem loading external script within react JSX

<script>
    (function(d) {
        var config = {
                kitId: 1234567,
                scriptTimeout: 3000,
                async: true
            },
            h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
    })(document);
</script>

and here is my render function where i would like to have the javascipt load asyn, it's super easy in a html file, however i am stunned within a react component on how to do acheive. (If i can avoid install another external module would be best). Many thanks

render() {
    return (
        <head>
            //Script to load asyn
        </head>
    )
}
Bill
  • 17,872
  • 19
  • 83
  • 131
  • What you're trying to do won't work. Instead try one of the following strategies inside `componentDidMount()` http://stackoverflow.com/questions/13121948/dynamically-add-script-tag-with-src-that-may-include-document-write – azium Jan 13 '16 at 06:44
  • Where is the react component being injected in your html? – hazardous Jan 13 '16 at 06:48
  • Yeah, I'm not clear on what you're trying to do. Can you provide a fiddle with your implementation that doesn't work? – Josh David Miller Jan 13 '16 at 06:49

2 Answers2

3

My server renders the initial HTML document, so I couldn't just insert it into the head as @mjhm suggested. Using @bluebill1049's answer I was able to make use of ES6 Template Strings and react's dangerouslySetInnerHTML

import React, {Component} from 'react';

export default class Html extends Component {

  render() {
    const loadTypeKit = `
      (function(d) {

        var config = {
            kitId: 'YOUR_KITID', 
            scriptTimeout: 3000, 
            async: true
          },
          h=d.documentElement,
          t=setTimeout(function() {
            h.className=h.className.replace(/wf-loading/g,"")+" wf-inactive";
          },config.scriptTimeout),
          tk=d.createElement("script"),
          f=false,
          s=d.getElementsByTagName("script")[0],
          a;

        h.className+=" wf-loading";
        tk.src='https://use.typekit.net/'+config.kitId+'.js';
        tk.async=true;
        tk.onload=tk.onreadystatechange=function() {
          a=this.readyState;
          if(f||a&&a!="complete"&&a!="loaded") return;
          f=true;
          clearTimeout(t);
          try{
            Typekit.load(config)
          } catch(e){ }
        };
        s.parentNode.insertBefore(tk,s);

      })(document);
      `;

    return (
      <html>
      <head>
        <script dangerouslySetInnerHTML={{__html: loadTypeKit}} />
      </head>
      <body>
          ...
      </body>
      </html>
    );
  }
}
Patrick Lee Scott
  • 8,217
  • 3
  • 36
  • 42
1

dangerouslySetInnerHTML is the solution i have found.

https://facebook.github.io/react/tips/dangerously-set-inner-html.html

This allow you to have script tag and insert JavaScript in side of jSX.

Bill
  • 17,872
  • 19
  • 83
  • 131
  • It would have been great if you updated your specific code example with the actual solution you used. Thanks for the pointer anyways. – Souvik Ghosh Jul 07 '19 at 16:24