Skip to content

Instantly share code, notes, and snippets.

@tmbtech
Created October 14, 2015 18:36
Show Gist options
  • Save tmbtech/ae3461276346c479e34e to your computer and use it in GitHub Desktop.
Save tmbtech/ae3461276346c479e34e to your computer and use it in GitHub Desktop.
Testing out Script tag
import React from "react";
import ReactDom from "react-dom";
class App extends React.Component {
state = {
loaded: false
}
onClick = () => {
this.setState({clicked: "sure"});
}
onLoaded = () => {
this.setState({loaded: true});
}
render() {
if (!this.state.loaded) {
return (<Script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.5/immutable.js" onLoad={this.onLoaded} />);
}
return (
<div>
{console.log(Immutable.Map({foo:"bar"}).toJS())}
<button onClick={this.onClick}>click me</button>
</div>
);
}
}
class Script extends React.Component {
static unload = () => {
console.log()
}
shouldComponentUpdate() {
return false;
}
componentDidMount() {
const {src, onLoad} = this.props;
this.loadScript(src)
.then(() => {
if (onLoad) {
onLoad();
}
});
}
loadScript(src) {
return new Promise(function (resolve, reject) {
let script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
render() {
return null;
}
}
ReactDom.render(<App />, document.getElementById("app"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment