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
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.
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.
Both React and the application code can stay as <script> tags with no changes.
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.
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?
Try the unoffical snapchat npm package. (no idea if the package does what you need, buy maybe you haven't seen it yet?)
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With