Adding script tag to React/JSX

后端 未结 17 944
感动是毒
感动是毒 2020-11-22 03:52

I have a relatively straightforward issue of trying to add inline scripting to a React component. What I have so far:

\'use strict\';

import \'../../styles/         


        
17条回答
  •  既然无缘
    2020-11-22 04:22

    The answer Alex Mcmillan provided helped me the most but didn't quite work for a more complex script tag.

    I slightly tweaked his answer to come up with a solution for a long tag with various functions that was additionally already setting "src".

    (For my use case the script needed to live in head which is reflected here as well):

      componentWillMount () {
          const script = document.createElement("script");
    
          const scriptText = document.createTextNode("complex script with functions i.e. everything that would go inside the script tags");
    
          script.appendChild(scriptText);
          document.head.appendChild(script);
      }
    

提交回复
热议问题