Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - Converting HTML script tag to load an SDK asynchronously

Currently I'm trying to add snapchat to a site through their SDK

my current attempt, can't figure out how to convert their Dom script into a function to be called when my component is loaded:

export function snapchatSDK() {
  return new Promise(resolve => {
    const script = document.createElement('script');
    script.src = 'https://sdk.snapkit.com/js/v1/create.js';
    document.head.append(script);
  });

}

class Platforms extends React.Component {

  componentDidMount() {
    snapchatSDK();
  }

render() {
    return (
      <div>
      <p> Share on Social Media Platforms</p>
      <h4>Snapchat<h4>
       <button 
         className="snapchat-creative-kit-share"
         data-share-url= urlTobeShared()
          >
         Share me 
       </button>

       <h4>Twitter<h4>
       <button>
         Share me 
       </button>

      <h4>Reddit<h4>
       <button>
         Share me 
       </button>
      </div>
    );
  }
}

Here is a link to the doc's as well: snap doc

like image 898
bruce Avatar asked Apr 18 '20 12:04

bruce


People also ask

How do you load third party scripts 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.

Can I convert HTML to React?

When you convert HTML website to ReactJS, you need to turn web pages into components. Furthermore, if you only have one page, you can create a folder called components under the SRC folder. Then, you need to create a single . JSX file there like index.

Can we use script tag in React?

Both React and the application code can stay as <script> tags with no changes.

How do you use JavaScript in JSX?

To add JavaScript code inside JSX, we need to write it in curly brackets like this: const App = () => { const number = 10; return ( <div> <p>Number: {number}</p> </div> ); }; Here's a Code Sandbox Demo. Inside the curly brackets we can only write an expression that evaluates to some value.


1 Answers

Your example doesn't make it very clear what you're trying to accomplish, but maybe one of these will help you figure it out?

Option 1

Try the unoffical snapchat npm package. (no idea if the package does what you need, buy maybe you haven't seen it yet?)

Option 2

Load the script in your HTML

<script src="https://sdk.snapkit.com/js/v1/create.js" />
<script src="/path/to/your/bundle.js" />

If loaded before your components mount, it should pick up your HTML and do whatever it does

Option 3

Maybe try setting async = false on the script:

const script = document.createElement('script');
script.src = 'https://sdk.snapkit.com/js/v1/create.js';
script.async = false
document.head.append(script);

Please see this article and this SO post. Key takeaway is:

Scripts that are dynamically created and added to the document are async by default

like image 67
Dana Woodman Avatar answered Sep 26 '22 04:09

Dana Woodman