Last active
October 14, 2015 18:56
-
-
Save tmbtech/4d22d319624a6d119752 to your computer and use it in GitHub Desktop.
Script tag wrapped
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDom from "react-dom"; | |
class App extends React.Component { | |
onClick = () => { | |
this.setState({clicked: "sure"}); | |
} | |
onLoaded = () => { | |
console.log("loaded"); | |
} | |
render() { | |
return ( | |
<div> | |
<Script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.5/immutable.js" onLoad={this.onLoaded}> | |
<View /> | |
</Script> | |
</div> | |
); | |
} | |
} | |
class View extends React.Component { | |
render() { | |
const foobar = Immutable.Map({foobar: "foobar"}).toJS(); | |
return ( | |
<div> something: {foobar.foobar} </div> | |
); | |
} | |
} | |
class Script extends React.Component { | |
state = { | |
loaded: false | |
} | |
shouldComponentUpdate(nextProps, nextState) { | |
return nextState.loaded; | |
} | |
componentDidMount() { | |
const {src, onLoad} = this.props; | |
this.loadScript(src) | |
.then(() => { | |
if (onLoad) { | |
onLoad(); | |
} | |
this.setState({loaded: true}); | |
}); | |
} | |
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() { | |
if (this.state.loaded === false) | |
return null; | |
return this.props.children; | |
} | |
} | |
ReactDom.render(<App />, document.getElementById("app")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment