Created
March 9, 2020 17:37
-
-
Save kayode-adechinan/75c1f7c32cd4633a023f740d86df3075 to your computer and use it in GitHub Desktop.
This file contains 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
const loadDynamicScript = (src, callback) => { | |
const existingScript = document.getElementById('scriptId'); | |
if (!existingScript) { | |
const script = document.createElement('script'); | |
script.src = src; // URL for the third-party library being loaded. | |
//script.id = 'libraryName'; // e.g., googleMaps or stripe | |
document.body.appendChild(script); | |
script.onload = () => { | |
if (callback) callback(); | |
}; | |
} | |
if (existingScript && callback) callback(); | |
}; | |
state = { | |
csReady:false, | |
ftReady:false, | |
interval: undefined | |
} | |
componentDidMount(){ | |
this.interval = setInterval(() => { | |
if(this.state.csReady == false || this.state.ftReady == false){ | |
loadDynamicScript('js/common_scripts.min.js', () => { | |
this.setState({ csReady: true }); | |
}); | |
loadDynamicScript('js/functions.js', () => { | |
this.setState({ ftReady: true }); | |
}); | |
} | |
}, 1); | |
} | |
componentWillUnmount() { | |
clearInterval(this.interval); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment