Created
August 23, 2020 04:49
-
-
Save junibrosas/c21014902789b3dde0dce934c3d60d71 to your computer and use it in GitHub Desktop.
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
/** | |
* Component that loads script. | |
* It gives you a callback, so you can continue doing stuff once script get loaded. | |
*/ | |
export interface mapWrapperProps{ | |
asyncScriptOnLoad?:()=>void; | |
libraries?:string; | |
} | |
class MapWrapper extends React.Component<mapWrapperProps,{}>{ | |
componentWillMount(){ | |
let scriptlibraries = this.props.libraries?"libraries="+this.props.libraries:""; | |
this.loadScript("https://maps.googleapis.com/maps/api/js?key=apikey&" + scriptlibraries, this.props.asyncScriptOnLoad); | |
} | |
loadScript(src,callback){ | |
let len = $('script').filter(function () { | |
return ($(this).attr('src') == src); | |
}).length; | |
if(len){ | |
if(callback)callback(); | |
}else { | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
if(callback)script.onload=callback; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
script.src = src; | |
} | |
} | |
render(){ | |
return (<div></div>); | |
} | |
} | |
export default MapWrapper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment