Adding script tag to React/JSX

后端 未结 17 1116
感动是毒
感动是毒 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:18

    You can find best answer at the following link:

    https://cleverbeagle.com/blog/articles/tutorial-how-to-load-third-party-scripts-dynamically-in-javascript

    const loadDynamicScript = (callback) => {
    const existingScript = document.getElementById('scriptId');
    
    if (!existingScript) {
        const script = document.createElement('script');
        script.src = 'url'; // URL for the third-party library being loaded.
        script.id = 'libraryName'; // e.g., googleMaps or stripe
        document.body.appendChild(script);
    
        script.onload = () => {
          if (callback) callback();
        };
      }
    
      if (existingScript && callback) callback();
    };
    

提交回复
热议问题