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>
    )
}
3
  • What you're trying to do won't work. Instead try one of the following strategies inside componentDidMount() stackoverflow.com/questions/13121948/… Commented Jan 13, 2016 at 6:44
  • Where is the react component being injected in your html? Commented Jan 13, 2016 at 6: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? Commented Jan 13, 2016 at 6:49

2 Answers 2

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>
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

It would have been great if you updated your specific code example with the actual solution you used. Thanks for the pointer anyways.

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.