Last active
February 14, 2020 15:51
-
-
Save shivampip/b7dafb500f3d53f5da79b18000c15065 to your computer and use it in GitHub Desktop.
[Medium] Use External Script with React
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 Script from "react-load-script"; | |
| import Demo from "./Demo"; | |
| class App extends React.Component { | |
| state = { scriptLoaded: false }; | |
| handleScriptError() { | |
| console.log("Error while loading script"); | |
| } | |
| handleScriptLoad() { | |
| console.log("Script loaded successfully"); | |
| this.setState({ scriptLoaded: true }); | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| <Script | |
| url="/assets/js/your-script.js" | |
| onError={this.handleScriptError.bind(this)} | |
| onLoad={this.handleScriptLoad.bind(this)} | |
| /> | |
| <Demo ready={this.state.scriptLoaded} /> | |
| </div> | |
| ); | |
| } | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment